Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Set Or List

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.

like image 998
SP M Avatar asked Feb 08 '12 06:02

SP M


People also ask

What is difference between list and bag in Hibernate?

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.

What is set in Hibernate?

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.

How Hibernate stores list values?

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.

Which collection is used for bidirectional collection of data?

So, for bidirectional collections, we could use a java. util. List or a java.

How do I map a list in hibernate?

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.

Is a list or a set more efficient in hibernate?

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.

What is the difference between list and bag in hibernate?

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.

What is Hibernate mapping in Java?

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.


Video Answer


1 Answers

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.

like image 84
nolt2232 Avatar answered Oct 31 '22 16:10

nolt2232