We're updating the issue view to help you get more done. 

CascadeType.PERSIST does not work over 3 level if last level uses IdClass

Description

A simple chain of 3 Entities with unidirectional ManyToOne associations and cascade = CascadeType.PERSIST to their parent entity works if every Entity has a simple id (also if they all have a sequence generator for that id).

Chain:

Company ---o Department ---o Employee

If the last Entity (most dependent) in the chain (here Employee) uses an IdClass annotation in order to reuse the id of its parent entity then:

  • a) The column for the company_id in department can not be updatable = false or nullable = false

  • b) If all Entity instances are new created and only Employee is persisted, then the Order of statements is:

  1. INSERT Department

  2. INSERT Employee

  3. INSERT Company

  4. Update for Department

Effects:

  • The foreign key column for the Department Id inside the Employee table must be nullable.

  • Unnecessary roundtrip to the database for the UPDATE statement.

Observed Behavior if company_id is not nullable:
org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity.

Expected Behavior:
Foreign key column in Department can be annotated with nullable = false.
No additional UPDATE will take place.

Environment

MacOS 10.14.5, openjdk version 11.0.2, hibernate-core-5.3.9, hibernate-java8-5.3.9, hibernate-validator-6.0.15, hibernate-commons-annotations-5.0.4, javax-persistence-api-2.2, Apache Derby 10.14.2.0

Status

Assignee

Unassigned

Reporter

Stefan Roeper

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.4.3
5.4.2
5.3.9

Priority

Major