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

Activity

Show:
Guillaume Smet
July 18, 2018, 9:33 AM

Ah OK . I saw you using @ManyToMany whereas the original case was about @OneToMany and I thought you were working on another case.

So, IIUC, the issue described here is fixed but you have another more specific one related to @ManyToMany and @JoinTable.

Could you check if the issue described in https://hibernate.atlassian.net/browse/HHH-7404 is also fixed? AFAICS, the main difference with the one of this issue is that they use a CascadeType.ALL. It might be fixed too.

If so, I think I would close these two and open a new one specific to your case as it seems to be different.

WDYT?

Petar Tahchiev
July 18, 2018, 9:58 AM

I think what you are saying is correct, however, I don't want to take the responsibility of closing other people's tickets. Yes, to me it seems like they are fixed, however it would be great if you could add the provided test-cases to the master branch (I tested them and they are green) before closing the tickets. Both, this one and HHH7404 seems to me are OK.
I will create a new ticket now for my case.

Petar Tahchiev
July 18, 2018, 10:03 AM
Guillaume Smet
July 18, 2018, 10:05 AM

perfect, thanks.

I will add the test cases and close the issues later today.

Guillaume Smet
July 20, 2018, 9:32 AM

This issue has already been fixed (no idea when). I added the provided test case to our test suite to prevent regressions.

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