Skip to content
Advertisement

hibernate multiple @OneToMany relationships generating more results in parent relationship

I will start with the result. In the next image I am displaying the result content of company.userAccount. In the database I have only two companies assigned to the same user and 7 services attached to one of the companies.

enter image description here

The generated query (simplified) is

This will generate

enter image description here

Expected result: I was expecting in the UserAccount to see only 2 companies and in one of the companies to have 6 services. Why do I have this result? Why is the same company object multiple times in the list? How can I avoid this?

I believe that one solution would be to change the fetching type of the @OneToMany relationships to LAZY (as they are default) (already tested this solution and it works) but what if I need this type of scenario?

Advertisement

Answer

Yes, there are 2 companies, just like you said. You can notice, that all objects are the same (hashes match). The reason behind this is that the query generates 6 rows for one company and PersistenceBag used by hibernate does not remove duplicates by default. Passing distinct to your sql would not work, because there is only one parent entity.

But this can be achieved by using hibernate’s query hints. Passing QueryHints.HINT_PASS_DISTINCT_THROUGH to your Query simply removes duplicated children.

For more information you can refer to this source.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement