I have been having problems with a entity manager merge() method.
There is two entities 'FOO' and 'BAR' they have no relationship in the schema it self and has to stay that way, sadly I cant change it. There is a column in FOO called PROCESS_CD which is exactly the same as the CLASS_CD in BAR. CLASS_CD is the primary key of BAR but PROCESS_CD in FOO is not a foreign key.
I want to update FOO's value in the PROCESS_CD from X to Y but hibernate tries to change the value in BAR as well and thus the error occurs.
I don't know how to specify that it should only update in FOO but not in BAR
So here is my database schema
My foo class
@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;
}
}
My bar class
@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;
}
}
This is my fooDao class
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();
}
}
and then lastly the methods I call to save
Here is the error I get
Caused by: org.hibernate.HibernateException: identifier of an instance of BarImpl was altered from X to Y
I hope you can help as I can't find anything on the internate
Thank you for your time