如何合并两个等于(但不相同)的分离实体?

时间:2013-11-27 09:36:05

标签: java hibernate merge

我有一个实体设置,归结为类似的东西:

Person (N:1) -> Job (1:N) -> Company

因此,某人有java.util.SetJob个实例。该集定义如下:

@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Job> jobs = new LinkedHashSet<>();

公司看起来像这样:

@Entity
public class Company {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false, unique = true)
    private String name;
    @OneToMany
    private List<Stelle> jobs = new ArrayList<Stelle>();

    public String getDisplayText() { return name; }
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result
            + ((getDisplayText() == null) ? 0 : getDisplayText().hashCode());
        return 31 * result + ((getId() == null) ? 0 : getId().hashCode());
    }
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Company))
           return false;
        return !(getId() == null && ((Company) obj).getId() != null)
             || getId() == null
             || (getId().equals(((Company) obj).getId());
    }
    // getters + setters
}

我在我的前端创建并将作业附加到人员实例。选择该公司并将其附加到具有Job的新javax.faces.Converter实体,并通过其ID从持久性上下文中获取该实体。

当我对同一entityManager.merge(person)的{​​{1}}个Job的人致电Company时,我会遇到以下异常:

Caused by: java.lang.IllegalStateException: Error occurred while storing 
  entity [example]. An entity copy [com.company.entity.Company#1] was already
  assigned to a different entity [example].
at org.hibernate.event.internal.EventCache.put(EventCache.java:192)     
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:287)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:153)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:851)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:833)   
at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:277)
[...]

我的问题似乎DefaultMergeEventListener.java:113(Hibernate-4.3.0.CR1)说:

if ( copyCache.containsKey( entity ) &&
        ( copyCache.isOperatedOn( entity ) ) ) {
    LOG.trace( "Already in merge process" );
    event.setResult( entity );
}
else { /* fetch entity and put it in copyCache */ }

copyCachejava.util.IdentityHashMap,因此我的两个分离实体 认为此缓存不相同,即使它们是根据equals()方法

我的问题很简单:我可以以某种方式继续使用上述流程或者我需要改变什么来让Hibernate合并这个实体图形?

已编辑添加:我知道Hibernate: An entity copy was already assigned to a different entityIllegalStateException: Error occurred while storing entity <entity> An entity copy <entity> was already assigned to a different entity <entity_copy>描述了类似的问题。

1 个答案:

答案 0 :(得分:2)

我之前遇到过这样的问题,我在hibernate文件:::

中使用了以下代码
Session session = getSession();
if(pojo.id!=null){
   session.merge(pojo)
}
else{
    session.saveOrUpdate(pojo)
}

其中getSession()是

public Session getSession() {
    Session session = getSessionFactory().getCurrentSession();
    if (session == null) {
        session = getSessionFactory().openSession();
    }
    return session;
}