Hibernate inserts duplicates into @OneToMany collection

Description

Consider following entities (getters and setters are omitted for brevity):

Now consider following code snippet, which persists one parent and one child entity and then prints the children of the parent.

The child entity is wrongly being inserted twice into the list of children of the parent entity.

When doing one of the following, the code works fine (no duplicate entries in the list):

  • remove the mappedBy attribute in the parent entity

  • perform some read operation on the list of children (e.g. uncomment line marked by *)

Also, when testing against another persistence provider, the code works as expected (no duplicates).

Take a look at axtavt's answer to my post on Stack Overflow. He seems to know why this problem occurrs.

I have attached a simple JUnit test for this issue.

Environment

MySQL 5.1.54

Assignee

Unassigned

Reporter

Damien

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

Priority

Minor
Configure