Problem with duplicates when fetching multiple many-to-many relations with Hibernate FetchMode.JOIN

Description

When there are multiple many-to-many relations with FetchMode.JOIN, session.find returns a result with a child relation of type List containing duplicates.

To avoid MultipleBagFetchException: cannot simultaneously fetch multiple bags when multiple @ManyToMany collections have @Fetch(FetchMode.JOIN), only one collection has type List and others have type Set.

With the following test data

a Book entity found by ID contains duplicates in List<Author> authors because Set<Category> categories has size 2: ["Software development", "System design"].

There are no duplicates in List<Author> authors when Set<Category> categories has size 1.

See the test .

The same problem is present when using Spring Data JPA (Spring Boot version - 2.2.6.RELEASE, Spring Data JPA version - 2.2.6.RELEASE, Hibernate version - 5.4.12.Final.

See the test .

Environment

Hibernate version - 5.4.12.Final
Java version - OpenJDK 11.0.6
H2 version - 1.4.200
OS - Ubuntu 18.04.3 LTS

Assignee

Unassigned

Reporter

Evgeniy Khyst

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure