OneToMany with referencedColumnName returns too many entities

Description

There seems to be a regression in 5.3.X from previous versions when using a OneToMany relation with referencedColumnName and EAGER fetching (it does not happen with LAZY fetching).

When fetching a parent instance with one or many children, Hibernate returns a random number of entities.

Parent:

1 2 3 4 5 6 7 8 @Id @Column(name = "PARENT_ID") private Long id; @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }) @JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID") @OrderBy("createdOn desc") private List<Child> children;

Child:

1 2 3 4 5 6 7 8 9 @Id @Column(name = "CHILD_ID") private Long id; @Column(name = "PARENT_ID") private Long parentId; @Column(name = "CREATED_ON") private ZonedDateTime createdOn;

When adding 2 children and 1 parent to the database, we end up with a List of 26 children in the parent.

This is fairly old code and could be done much better but with any version lower than 5.3.X it works fine.

This has been tested with Postgresql, Oracle and H2 (in memory) and is always reproducible.

I have attached a test (maven setup).

Environment

Java 8, Java 10, Spring 5.X

Status

Assignee

Guillaume Smet

Reporter

Michel Zimmermann

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.2.17

Components

Affects versions

5.3.7
5.3.3
5.3.4
5.3.6
5.3.1
5.4.0
5.4.0.CR1
5.3.2
5.3.0.Final
5.3.5

Priority

Critical