Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Persisting new object with references to detached entities - fails(Persisting new object with (cascade.PERSIST etc) references to detached entities - fails as it tries to INSERT the detached entities)
icon5.gif  Persisting new object with references to detached entities - fails [message #666256] Tue, 19 April 2011 12:03 Go to next message
Eclipse UserFriend
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 12:23] by Moderator

Re: Persisting new object with references to detached entities - fails [message #666509 is a reply to message #666256] Wed, 20 April 2011 11:48 Go to previous message
Eclipse UserFriend


This is resolved by just using merge with the caveat that the merged entity must be returned and the pre-merged object isn't used anywhere.
Previous Topic:Configure JTA Datasouce in Persistence.xml
Next Topic:@TableGenerator generates duplicate primary keys when used concurrently
Goto Forum:
  


Current Time: Mon Jul 07 12:35:25 EDT 2025

Powered by FUDForum. Page generated in 0.03812 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top