我一直遇到實體管理器merge()方法的問題。HibernateException:foo實例的標識符已從X更改爲Y
有兩個實體'FOO'和'BAR',它們在模式中沒有關係,只能保持這種方式,可悲的是我不能改變它。 FOO中有一個名爲PROCESS_CD的列,與BAR中的CLASS_CD完全相同。 CLASS_CD是BAR的主鍵,但FOO中的PROCESS_CD不是外鍵。
我想將PROCESS_CD中FOO的值從X更新爲Y,但是hibernate會嘗試更改BAR中的值,從而發生錯誤。
我不知道如何指定它應該在FOO只更新而不是在BAR
因此,這裏是我的數據庫架構
我的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
我希望你能幫忙,我可以」 t找到任何東西在國際
謝謝你的時間
嗯還是找不到問題 – SandMan