1

There are many questions on stackoverflow that speaks about using List vs Set in Hibernate entities. My question is slightly different (or may be same, but I am not able to understand it yet).

If I read the Hibernate docs, they recommend to use Set for many-valued associations here http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#persistent-classes-equalshashcode.

Quoting from above documentation link:

 You have to override the equals() and hashCode() methods if you:

 - intend to put instances of persistent classes in a Set (the recommended way to represent many-valued associations); 
 - and intend to use reattachment of detached instances

That was till Hibernate 4.3. But in later versions, I don't see that statement anymore (http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#mapping-model-pojo-equalshashcode)

So my questions are

  • why did Hibernate recommended to use Set instead of List till Hibernate 4?
  • Since I don't see that recommendation in Hibernate 5, does it mean that it is not valid anymore?

Thanks!!

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
adi
  • 430
  • 1
  • 5
  • 10
  • I don't think there is just a single rule what you should do, but the usage is depending on your need. I suggest reading this, but you probably already did - http://stackoverflow.com/questions/6562673/onetomany-list-vs-set-difference – Shadov May 02 '17 at 09:49
  • Thanks for commenting. I read that question, and some others too. I also agree that there isn't just a single rule. I am trying to understand why exactly Hibernate recommended the usage of Sets. I believe there should be some reason behind such a recommendation. – adi May 02 '17 at 09:56

1 Answers1

1

Well to answer your first question:

why did Hibernate recommended to use Set instead of List till Hibernate 4?

I will start by pointing out the main differences between Listand Set:

  • List is an ordered collection while Set is not, but note that while List is an ordered collection, it won't be ordered if no index column is specified in your object.
  • Set doesn't allow duplicates while List does.

And this second point is what explains the recommendation of using Set upon List, even if Hibernate documentation itself says that lists and bags are more efficient than sets.

In fact the problem with using List in bi-directional relationships is that when you merge operation is called on the parent association it inserts duplicate children, which seems to be a bug in older Hibernate versions, you can read more about this problem on:

Hibernate Facts: Favoring bidirectional Set(s) vs List(s)

And to answer your second question:

Since I don't see that recommendation in Hibernate 5, does it mean that it is not valid anymore?

We can't make a deduction in this fact but I think this bug is fixed on Hibernate 5 that's why we don't see this recommendation anymore, but it's just an assumption.

Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78