Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Many to Many Relations Set Or List?

I have a many to many relationship at my Java beans. When I use List to define my variables as like:

@Entity @Table(name="ScD") public class Group extends Nameable {      @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)     @JoinColumn(name="b_fk")     private List<R> r;     //or     private Set<R> r; 

I get that error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' ... 

When I use Set everything seem to work well.

I want to ask that when using many to many relationships which one to use for logical consept List or Set (because of list may have duplicates and set but what about performance and other issues)?

like image 847
kamaci Avatar asked Nov 17 '11 21:11

kamaci


People also ask

How many-to-many relationship works in hibernate?

To demonstrate many to many mapping using hibernate annotations, we will associate two entities i.e. ReaderEntity and SubscriptionEntity .

How many tables are created in many-to-many relationship hibernate?

Hibernate creates two tables in a many to many relationship.

How do you map a many-to-many relationship?

When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.

How do you persist many-to-many relationships in JPA?

In JPA we use the @ManyToMany annotation to model many-to-many relationships. This type of relationship can be unidirectional or bidirectional: In a unidirectional relationship only one entity in the relationship points the other. In a bidirectional relationship both entities point to each other.

Should hibernate use a set or a list?

Databases do not preserve order and using a List is meaningless, the order in them is unspecified (unless using so called indexed collections ). Using a Set also has great performance implications. When List is used, Hibernate uses PersistentBag collection underneath which has some terrible characteristics.

How does hibernate deal with @manytomany relationships?

First of all, keep in mind that Hibernate deals with @ManyToMany relationships as two unidirectional @OneToMany associations. The owner-side and the child-side (the junction table) represents one unidirectional @OneToMany association.

How to use @manytomany annotation in hibernate?

When using the @ManyToMany annotation, always use a java.util.Set. Do not use the java.util.List. In the case of other associations, use the one that best fits your case. If you go with List, do not forget to be aware of the HHH-58557 issue that was fixed starting with Hibernate 5.0.8.

Can we use list or set in @manytomany relationships in JPA?

In this article, we'll highlight the performance penalties involves by using List or Set in @ManyToMany relationships. We use Spring Data JPA with the default persistence provider, therefore with Hibernate JPA. First of all, keep in mind that Hibernate deals with @ManyToMany relationships as two unidirectional @OneToMany associations.


2 Answers

From relational databases perspective this is a set. Databases do not preserve order and using a List is meaningless, the order in them is unspecified (unless using so called indexed collections).

Using a Set also has great performance implications. When List is used, Hibernate uses PersistentBag collection underneath which has some terrible characteristics. I.e.: if you add a new relationship it will first delete all existing ones and then insert them back + your new one. With Set it just inserts the new record.

Third thing - you cannot have multiple Lists in one entity as you will get infamous cannot simultaneously fetch multiple bags exception.

See also:

  • 19.5. Understanding Collection performance

  • Why Hibernate does "delete all then re-insert" - its not so strange

like image 85
Tomasz Nurkiewicz Avatar answered Sep 29 '22 12:09

Tomasz Nurkiewicz


How about the uniqueness requirement from Set? Doesn't this force Hibernate to retrieve all objects each time one is added to the collection to make sure a newly added one is unique? A List wouldn't have this limitation.

like image 41
Marc Avatar answered Sep 29 '22 12:09

Marc