Sets in Hibernate suffer because you can store an object before it was updated and Hibernate will update it.
E.g. If say
Person {
id: Integer,
name: String
}
Let's say I have some entity that stores Persons as HashSet. If I add them before saving, their hash set is taken without Id.Id can be automatically generated by Hibernate on save/create.
After save the Id now has a new value. I.e. its hash differs from when it was saved.
Boom. You have HashSet with Zombie entries. That are technically that you can't fetch actually.
Re:Problems with JPA/Hibernate
by doctor_eval on Monday April 12, 2021 @01:54AM
(#26776393)
Pretty sure I would have used a list, but making the Java interface ordered surely doesn’t change the semantics of the underlying relation?!
Re:Problems with JPA/Hibernate
by tablespoon on Monday April 12, 2021 @11:29AM
(#26780471)
> Pretty sure I would have used a list, but making the Java interface ordered surely doesn’t change the semantics of the underlying relation?!
Yeah, it seems a little bonkers on first glance and I haven't ever dug in to see if there was a justifiable reason or not.
IIRC, if you just need ordering, you could just use an SortedSet. I think technically lists are for when you need ordering and duplicates.
Re:Problems with JPA/Hibernate
by doctor_eval on Monday April 12, 2021 @08:12PM
(#26786687)
Yeah it is bonkers! Not just at first glance. The fact that the SQL integration for the child object behaves differently based on the kind of collection used in the parent is exactly the kind of unintuitive behaviour that drove me nuts.
There are so many rules to remember for something that is supposed to make SQL integration natural.
IIRC, it does that if you use Lists instead of Sets.
https://dzone.com/articles/best-performance-practices-for-hi...