Hi, Both entities belong to different persistence units and have their own entity managers. I have pasted the persistence.xml snippet below for both the persistence files. The Zone entity in persistenceUnit1 has a field property of Policy entity also in persistenceUnit1. This Policy entity is a base class of another entity CustomPolicy which is in persistenceUnit2. I am trying to persist the Zone with entity manager of persistenceUnit1. This is when I hit the problem. If we cannot persist this way, then what could be solution to this issue.
<persistence-unit name="PersistenceUnit1"> <jta-data-source>jdbc/db2connection</jta-data-source> <class>mypack.src.Policy</class> <class>mypack.src.Zone</class> <exclude-unlisted-classes>false</exclude-unlisted-classes> <validation-mode>NONE</validation-mode> <properties> <!-- Create the database tables on startup --> <property name="openjpa.jdbc.SynchronizeMappings" value= "buildSchema(ForeignKeys=true)"/> <property name="openjpa.Log" value="DefaultLevel=ERROR"/> <property name="openjpa.jdbc.DBDictionary" value="db2"/> <property name="openjpa.Multithreaded" value="true"/> <property name="openjpa.jdbc.TransactionIsolation" value="read-committed" /> </properties> </persistence-unit> <persistence-unit name="PersistenceUnit2"> <jta-data-source>jdbc/db2connection</jta-data-source> <class>mycustom.src.CustomPolicy</class> <class>mypack.src.Policy</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <validation-mode>NONE</validation-mode> <properties> <!-- Create the database tables on startup --> <property name="openjpa.jdbc.SynchronizeMappings" value= "buildSchema(ForeignKeys=true)"/> <property name="openjpa.Log" value="DefaultLevel=ERROR"/> <property name="openjpa.jdbc.DBDictionary" value="db2"/> <property name="openjpa.Multithreaded" value="true"/> <property name="openjpa.jdbc.TransactionIsolation" value="read-committed" /> </properties> </persistence-unit> Regards, Akash From: Rick Curtis <curti...@gmail.com> To: users <users@openjpa.apache.org>, Date: 10/22/2013 12:42 AM Subject: Re: JPA exception while persistence into database > That entity has a field associated with another entity. Both these entities belong to different persistence units. You can't persist a relationship from an Entity to another Entity that belongs in a different persistence units. An EntityManager will only operate against Entities that are a part of it's persistence unit definition. Do you truly have two different persistence units, or do you just have two different EntityManagers? On Mon, Oct 21, 2013 at 11:50 AM, Akash Gunjal <akgun...@in.ibm.com> wrote: > Hi, > > I am trying to persist an entity into the database. That entity has a field > associated with another entity. Both these entities belong to different > persistence units. When trying to persist the entity I get the below error. > > > Invalid parameter - Encountered unmanaged object > "com.ibm.powersc.ts.policies.vlan.persistence.VLANPolicy@40004000" in life > cycle state unmanaged while cascading persistence via field > "com.ibm.sc.core.persistence.Zone.policies<element:class > com.ibm.sc.core.persistence.Policy>" 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 tried to give cascade.All option to the field having entity reference, > then I get an error message "This entity is not managed by this context". > > Regards, > Akash > > -- *Rick Curtis*