【发布时间】: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 中
这是我的数据库架构
我的 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