Java JPA-使用多个一对多关联更新实体的最佳方法
执行具有多个一对多关联(>8)的实体更新的最佳方式是什么 我尝试了多种方法,比如使用Jpa和hibernate合并,但它首先插入新的子条目,然后删除旧条目。我需要检查其中一个子条目是否已更改,如果未更改,则更新它,如果未更改,则不执行任何操作 我的对象看起来像:Java JPA-使用多个一对多关联更新实体的最佳方法,java,hibernate,spring-boot,jpa,one-to-many,Java,Hibernate,Spring Boot,Jpa,One To Many,执行具有多个一对多关联(>8)的实体更新的最佳方式是什么 我尝试了多种方法,比如使用Jpa和hibernate合并,但它首先插入新的子条目,然后删除旧条目。我需要检查其中一个子条目是否已更改,如果未更改,则更新它,如果未更改,则不执行任何操作 我的对象看起来像: public class Parent { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long parentId; private String at
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long parentId;
private String attribute;
@OneToMany(
mappedBy = "parent",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<Child1> child1= new HashSet<>();
@OneToMany(
mappedBy = "parent",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<Child2> child2= new HashSet<>();
...
// other associations are removed because they are the same
//Constructors, getters and setters removed for brevity
}
public class Child1{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long child1Id;
private String attribute;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Parent parent;
//Constructors, getters and setters removed for brevity
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PostComment )) return false;
return this.attribute != null && this.attribute.equals(((Child1) o).getAttribute());
}
@Override
public int hashCode() {
return 31;
}
}
公共类父类{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长父ID;
私有字符串属性;
@独身癖(
mappedBy=“parent”,
cascade=CascadeType.ALL,
孤立删除=真
)
private Set child1=新HashSet();
@独身癖(
mappedBy=“parent”,
cascade=CascadeType.ALL,
孤立删除=真
)
private Set child2=新HashSet();
...
//其他关联将被删除,因为它们是相同的
//为了简洁起见,移除了构造函数、getter和setter
}
公营儿童1{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长童;
私有字符串属性;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“parent\u id”)
私人家长;
//为了简洁起见,移除了构造函数、getter和setter
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(!(o PostComment实例))返回false;
返回this.attribute!=null&&this.attribute.equals((Child1)o.getAttribute());
}
@凌驾
公共int hashCode(){
返回31;
}
}
所有其他孩子看起来都很相似,都有相同的多对一关联和属性
欢迎大家帮忙!谢谢