Let's suppose every Product has a ProductCategory. Product categories very 
rarely change so I load those objects at startup from the database with JPA, 
detach them from their entity manager, cache them outside of JPA, then 
occasional re-use them in a read-only capacity on other entities. Older 
versions of OpenJPA seemed to simply skip over those detached entities while 
persisting the parent...in this example Product.ProductCategory would get 
skipped. Something changed (in WebSphere 7 to 8.5.5 if anyone cares about 
specifics) such that I now receive this error message:

<openjpa-2.2.2-r422266:1468616 fatal user error> 
org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged 
object "com.example.ProductCategory@166184d" in life cycle state  unmanaged 
while cascading persistence via field "com.example.Product.productCategory" 
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().
FailedObject: com.example.ProductCategory@166184d

I don't want to persist the detached child field, nothing will ever change on 
it, and it doesn't belong to any entity manager scope. I want to ignore it when 
the main object is persisted.

Why isn't there a CascadeType.IGNORE that I could place on 
Product.ProductCategory to have OpenJPA revert to the old behavior of skipping 
over the detached entity?

Any ideas?

Reply via email to