• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unmanaged object exception persisting entity with FK to reference data

 
radhik va
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am currently reviving some code that was released in 2009 for WebSphere Process Server 6.1.2 using openJPA1.0 and DB2. I am not changing the underlying database for now and have not regenerated the entity classes.
I am now on Process Server 8.5.6 and updated the openJPA libraries to 2.4. I have also updated the persistence.xml with the necessary backward compatibility properties. The intent for now is to get the code running at the latest version.
I have an address entity that has a manytoone relation with the addresstype entity(reference data) and to contact entity. The contact entity in turn has onetomany relation with party entity.

When I try to persist a party, I get the following error
<openjpa-2.2.3-SNAPSHOT-r422266:1642995 fatal user error> org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged object "com.abc.persistence.entities.MAddressType-1" in life cycle state unmanaged while cascading persistence via field "com.abc.persistence.entities.Address.addressTypeId" during flush. However, this field does not allow cascade persist. You cannot flush unmanaged objects or graphs that have persistent associations to unmanaged objects.
Suggested actions: a) Set the cascade attribute for this field to CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or "all" (JPA orm.xml),
b) enable cascade-persist globally,
c) manually persist the related field value prior to flushing.
d) if the reference belongs to another context, allow reference to it by setting StoreContext.setAllowReferenceToSiblingContext().


I dont want to cascade persist to the addresstype as it already exists in the DB and is a reference/master data. When setting the addresstype object on the address, I am finding the entity using the key and setting it on the address object. I am not a pro with opeJPA so please bear with me. Let me know if you need any further details. Thanks

Attaching the entity class for address and addresstype and persistence.xml

Address class

AddressType class


Persistence.xml


 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the code where you get hold of the address type and assign it?
How do you manage your sessions?

I'm wondering if you picked up the address type in a different session.
 
radhik va
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is creating an EntityManager from a static method that returns the EntityManagerFactory for the persistentUnit.

Initially, the addresstype was set as a new entity of addresstype with the required values and set on the address entity. Since the same error was appearing, changed it to find (entitymanager.find(class, id) ) and set it on the address object. However. the error remains.


Code earlier :


Code now :


This address object is set on a party object. A new entitymanager is created and entityManager.persist(party) is called.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
radhik va wrote:This address object is set on a party object. A new entitymanager is created and entityManager.persist(party) is called.


Does that mean a new session is opened?
That would probably be your issue.
 
radhik va
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave. will check using the same entitymanager.
Do you see any issue with using the "Code before" ?
 
radhik va
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave, that worked, Wonder if this was not an issue in JPA1.x..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic