Can anybody know when to use Set and when to use List in hibernate mapping file ?
<set name="" table="">
<key>
<column name="" />
</key>
</set>
<list name="" cascade="all">
<key column="" />
<index column="" />
<one-to-many class=""/>
</list>
Thanks.
Hibernate's naming of the different collection types is a little bit confusing because Lists and Bags are both mapped by a java. util. List. The difference between them is that a List is ordered and a Bag is unordered.
Set is a collection of objects in which duplicate values are not allowed and the order of the objects is not important. Hibernate uses the following annotation for mapping sets: @ElementCollection – Declares an element collection mapping. The data for the collection is stored in a separate table.
The <key> element is the column in the CERTIFICATE table that holds the foreign key to the parent object i.e. table EMPLOYEE. The <list-index> element is used to keep the position of the element and map with the index column in the collection table. The index of the persistent list starts at zero.
So, for bidirectional collections, we could use a java. util. List or a java.
Define Hibernate Mapping File Let us develop our mapping file, which instructs Hibernate how to map the defined classes to the database tables. The <list> element will be used to define the rule for List collection used. The index of list is always of type integer and is mapped using the <list-index> element.
A List might be more efficient than a Set, but the type also influences how Hibernate manages the association in the database. So, there are a few other things you need to take into account when you make your decision. First of all, if you’re using a Hibernate version older than 5.0.8, you should be aware of bug HHH-5855.
Hibernate’s naming of the different collection types is a little bit confusing because List s and Bag s are both mapped by a java.util.List. The difference between them is that a List is ordered and a Bag is unordered.
The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. The <generator> element within the id element is used to generate the primary key values automatically.
Take a look at this post: @OneToMany List<> vs Set<> difference
The main difference is that a list has ordering while a set does not. Also, a set cannot have duplicate values, while a list can.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With