Persisting new object with references to detached entities - fails [message #666256] |
Tue, 19 April 2011 16:03 |
Caspar MacRae Messages: 12 Registered: March 2011 Location: London |
Junior Member |
|
|
We are using a rather horrible framework for our frontend that serializes lots of things into a page map, recently I've migrated the backend to OSGi with EclipseLink 2.3.0-M5
I have the following issue, when we try to persist a new instance that has references to detached entities it fails as tries to insert these references.
An example would be a new instance of a Person with detach entity of Address, where Person has an Address field annotated with
@ManyToOne(targetEntity = Address.class, cascade = {CascadeType.MERGE, CascadeType.REFRESH})
Users can also create a new Address to be persisted along with the Person (much of the frontend follows this style)
In the code below the EntityManager is taken from a ThreadLocal, so it's the same one being used to allow us to have EntityTransactions spanning multiple DAOs:
@Override
public <T> void save(T object) {
EntityManager em = getEntityManager();
EntityTransaction et = em.getTransaction();
boolean doCommit = false;
if(!et.isActive()) {
et.begin();
doCommit = true;
}
if(((DomainObject<?>)object).getId() == null) {
em.persist(object);
}else {
if(!em.contains(object)) {
object = em.merge(object);
}
}
if(doCommit) {
et.commit();
}
}
Any advice on how to get round this or what I'm doing wrong? If I remove the Cascadetype.PERSIST the the save new with detached works fine, but obviously we'd lose the new field (I can't get the time to change the frontend to persist these separately)
[Updated on: Tue, 19 April 2011 16:23] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03280 seconds