Merged transient entity may not contain some associations when passed to PrePersist/PostPersist callbacks

Description

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;
4) all inserts in the ActionQueue are executed
5) PrePersist callbacks are called with the new entity as argument;
6) the new entity is inserted.
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.

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.

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 because "non-owned" associations will not be copied to these entities when 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.

For more details see: https://bugzilla.redhat.com/show_bug.cgi?id=1245082

Environment

None

Assignee

Gail Badner

Reporter

Ryan Emerson

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure