如何强制Hibernate在更新之前删除孤儿 [英] How to force Hibernate to remove orphans before update
问题描述
@Entity
@Table(....)
public class AnnotationGroup {
...
private List< AnnotationOption>选择;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER,orphanRemoval = true)
@JoinColumn(name =annotation_group_id,nullable = false)
public List< AnnotationOption> getOptions(){
返回选项;
@Entity
@Table(...)
public class AnnotationOption {
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Override
public Long getId(){
return id;
目前我有 group1
with AnnotationOption
s opt1
opt2
和 opt3
然后我想用所有选项替换所有选项 opt1
另外我在数据库中有约束:
$ p $ CONSTRAINTUQ_ANNOTATION_OPTION_name_annotation_group_idUNIQUE(annotation_option_name,annotation_group_id)
而这一次引发:
:org.postgresql.util.PSQLException:错误:重复键值违反唯一约束UQ_ANNOTATION_OPTION_name_annotation_group_id
详细信息:键(name,annotation_group_id)=(opt1,3)已经存在。
实际上,hibernate会在更新后删除孤儿。
你能提出一些解决问题的方法吗?
p>
解决这个问题的最好方法是将现有子集与传入子集显式合并,以便:
- 将新的子实体添加到集合中。 删除不再需要的子实体。 / li>
- 更新与商业关键字(
annotation_group_name
,study_id
)匹配的子实体与传入的数据。
- 更新与商业关键字(
有关更多详细信息,请查看高性能Java持久性。
Let's say I have following model structure:
@Entity
@Table(....)
public class AnnotationGroup{
...
private List<AnnotationOption> options;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "annotation_group_id", nullable = false)
public List<AnnotationOption> getOptions() {
return options;
}
}
@Entity
@Table(...)
public class AnnotationOption {
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Override
public Long getId() {
return id;
}
}
At the moment I have group1
with AnnotationOption
s opt1
opt2
and opt3
Then I want to replace all option with only one option opt1
Additionally I have constraint in database:
CONSTRAINT "UQ_ANNOTATION_OPTION_name_annotation_group_id" UNIQUE (annotation_option_name, annotation_group_id)
And this one fires:
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "UQ_ANNOTATION_OPTION_name_annotation_group_id"
Detail: Key (name, annotation_group_id)=(opt1, 3) already exists.
Actually isuue that hibernate removes orphans after update.
Can you suggest something t resolve issue?
There are so many things that are wrong in this example:
- EAGER fetching on the
@OneToMany
collection is almost always a bad idea. - Unidirectional collections are also bad, use the bidirectional one.
- If you get this exception, most likely you cleared all the elements and re-added back the ones that you want to be retained.
The best way to fix it is to explicitly merge the existing set of children with the incoming ones so that:
- New child entities are being added to the collection.
- The child entities that are no longer needed are removed.
- The child entities matching the business key (
annotation_group_name
,study_id
) are updated with the incoming data.
For more details, check out High-Performance Java Persistence.
这篇关于如何强制Hibernate在更新之前删除孤儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!