This can happen when merging an entity has a "post-insert" ID that requires an insert to obtain the ID (e.g., identity ID).
Currently, when a transient entity with a post-insert ID is merged: 1) a new entity is instantiated; 2) merge operation is cascaded to associations with {{AssociationType.getForeignKeyDirection() == ForeignKeyDirection#FROM_PARENT}} 3) basic properties and associations with {{AssociationType.getForeignKeyDirection() == ForeignKeyDirection#FROM_PARENT}} are copied to the new entity; 3 4 ) all inserts in the ActionQueue are executed ; 4 5 ) PrePersist callback is callbacks are called prior to inserting with the new entity as argument ; 6) the new entity is inserted . 4 7 ) PostPersist callbacks are called with the new entity as argument; 8) merge is cascaded to associations with {{AssociationType.getForeignKeyDirection() == ForeignKeyDirection#TO_PARENT}} ; 9) associations with {{AssociationType.getForeignKeyDirection() == ForeignKeyDirection#TO_PARENT}} are copied to the new entity. If In 5) and 7), the entity passed to PrePersist/PostPersist callbacks do not have all associations copied yet. The values for the uncopied properties will be as set by the no-arg constructor used to instantiate the entity.
Because Validator Other transient entities still in the process of being merged that have inserts in the ActionQueue will be affected in the same way, even if they do not have post-insert IDs. This is a PrePersist Merge operation throws a ConstraintValidationException on because " non- empty list marked as owned" associations will not be copied to these entities when using the insert action is executed due to 4)..
An example where this is a problem is when merging a transient entity with a @OneToMany & @NotNull on a collection. If the collection is not initialized when the entity is instantiated, the PrePersist callback to Validator will throws a ConstraintValidationException. This is because the collection in the entity passed to {{Session#merge}} has not been copied to the newly instantiated entity yet (this is done at step 9)). As a result, "null" is always passed to the validator during a merge operation , even when the collection is non-empty, causing this operation to fail with C . V.E. For more details see: https://bugzilla.redhat.com/show_bug.cgi?id=1245082
|