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

Probably, because categories has size 2:

When there are 2 authors and 2 categories there are no duplicates in child relations.

Beginning transaction with session.beginTransaction() doesn't have any effect on the duplicates.

See the test .

Using Spring Data JPA and making the test transactional with @org.springframework.transaction.annotation.Transactional solves the problem with duplicates. Spring Boot version - 2.2.1.RELEASE, Spring Data JPA version - 2.2.1.RELEASE, Hibernate version - 5.4.8.Final.

See the test .

Environment

Hibernate version - 5.4.9.Final
Java version - OpenJDK 11.0.4
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