如何强制Hibernate在更新之前删除孤儿 [英] How to force Hibernate to remove orphans before update

查看:210
本文介绍了如何强制Hibernate在更新之前删除孤儿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @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>


  1. @OneToMany 集合上的EAGER抓取是几乎总是一个坏主意
  2. 单向集合也很糟糕,请使用双向

  3. 如果您遇到此异常,很可能您会清除所有元素,并重新添加要保留的元素。

解决这个问题的最好方法是将现有子集与传入子集显式合并,以便:


  1. 将新的子实体添加到集合中。 删除不再需要的子实体。 / li>
  2. 更新与商业关键字( 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 AnnotationOptions 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:

  1. EAGER fetching on the @OneToManycollection is almost always a bad idea.
  2. Unidirectional collections are also bad, use the bidirectional one.
  3. 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:

  1. New child entities are being added to the collection.
  2. The child entities that are no longer needed are removed.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆