【问题标题】:HibernateException: identifier of an instance of foo was altered from X to YHibernateException:foo 实例的标识符从 X 更改为 Y
【发布时间】:2016-09-28 08:04:34
【问题描述】:

我遇到了实体管理器 merge() 方法的问题。

有两个实体 'FOO' 和 'BAR' 他们在它自己的架构中没有关系并且必须保持这种状态,遗憾的是我无法更改它。 FOO 中有一个名为 PROCESS_CD 的列,它与 BAR 中的 CLASS_CD 完全相同。 CLASS_CD 是 BAR 的主键,但 FOO 中的 PROCESS_CD 不是外键。

我想将 PROCESS_CD 中 FOO 的值从 X 更新为 Y,但 hibernate 也尝试更改 BAR 中的值,因此发生错误。

我不知道如何指定它应该只在 FOO 中更新,而不是在 BAR 中

这是我的数据库架构

Database Schema

我的 foo 课

@javax.persistence.Entity
@Table(name = "FOO")
public class FooImpl implements Foo {

    private Long callCode;
    private Journal journal;

    @Id
    @Column(name = "CALL_CD")
    public Long getCallCode() {
        return callCode;
    }

    public void setCallCode(Long aLong) {
        callCode = aLong;
    }


    @ManyToOne(fetch = FetchType.LAZY, targetEntity = BarImpl.class)
    @JoinColumn(name = "PROCESS_TYPE")
    public Journal getJournal() {
        return journal;
    }
}

我的酒吧班

@javax.persistence.Entity
@Table(name = "BAR")
public class BarImpl implements Bar {

    private String Code;
    private String Description;

    public List<Interaction> interaction;

    @Id
    @Column(name = "CLASS_CD")
    public String getCode() {
        return Code;
    }

    public void setCode(String code) {
        Code = code;
    }

    @Column(name = "CLASS_LONG_DESCR")
    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }
}

这是我的 fooDao 课

public class FooDao {

    public void saveFoo(Foo instance) {
        log.debug("update instance");
        try {
            if (getEntityManager().contains(instance)) {
                getEntityManager().merge(instance);
            } else {
                getEntityManager().persist(instance);
            }
            log.debug("update successful");
            return instance;
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public void startTransaction() throws TransactionException{
        EntityTransaction t =  getEntityManager().getTransaction();
        t.begin();
        TransactionManager.setTransaction(t);
    }
    public void commitTransaction()throws TransactionException {
        EntityTransaction t = TransactionManager.retrieveTransaction();
        if(t == null)
            throw new TransactionException("No transaction associated witht his thread");
        t.commit();
    }
    public void rollbackTransaction() throws TransactionException{
        EntityTransaction t = TransactionManager.retrieveTransaction();
        if(t == null)
            throw new TransactionException("No transaction associated witht his thread");
        t.rollback();
    }
}

最后是我调用的保存方法

这是我得到的错误

Caused by: org.hibernate.HibernateException: identifier of an instance of BarImpl was altered from X to Y

希望你能帮忙,因为我在国际上找不到任何东西

感谢您的宝贵时间

【问题讨论】:

  • 嗯还是找不到问题

标签: java oracle hibernate exception persistence


【解决方案1】:

首先感谢所有回答我问题的人,你们都非常有用。 :D

我发现了我的问题,它与我的实际实现有关。我没有创建一个新的 barImpl,而是去搜索一个,如果它为 null,则需要创建一个新的 bar,但如果找到一个,它只会继续并输入新值。

看到我不知道实体类与数据库中的实际记录保持关系。因此,当我更改 DAO 找到的实体的值时,它实际上是在尝试保存 foo 实体时更改数据库中的值。

祝你有美好的一天

【讨论】:

    最近更新 更多