I have a standard JEE7 application using Eclipse Link
as JPA
engine and an Entity tree will all entities annotated with CascadeType.ALL
in their OneToMany
relationships. All Entities have @GeneratedValue
ids from database sequences.
During a transaction I have fetched a managed instance of an Entity tree, then I detach the root then set null to all @Id
fields and @Version
fields of the tree and merge the root Entity.
As I expect all managed Entities in the tree, except from two, have new Ids. The problem is that 2 entities of different type retain the null in their id fields and when committing I'm getting the following Exception:
org.eclipse.persistence.exceptions.ValidationException
Exception Description: Null or zero primary key encountered in unit of work clone [EntityA [id=null, businessId=17EN000000000083]], primary key [null]
I tried several changes in equals and hashCode methods in that EntityA mainly to avoid regarding two entities equals if their ids are null in both of them, but nothing happened. The behavior is not deterministic, during debugging sometimes the id was generated but usually is null.
Could anyone give a hint for the problem.
--EDIT
When we used FetchType.EAGER
instead of FetchType.LAZY
in problematic relationship, the merge
created the ids as expected. Could anyone explain why this happens?