Set mappings for Hibernate hibernate and sort, order-by properties

Source: Internet
Author: User
Tags set set

Common set Set, List, Map, I believe everyone is very familiar with the interview will often ask. Both the set and list inherit the collection interface, set is unordered and cannot be duplicated, and the same elements cannot be stored, whereas lsit is stored sequentially and can store duplicate elements. Map is not inherited collection, map provides a key to value mapping, map is also non-repeatable (key cannot be duplicated), a map cannot contain the same key, each key can only map one value.

When the properties of a persisted class are collections, to store them in a database, you need to add a database table to complete the mapping work. For example, a student may have more than one educational background, this time the properties of the persistence class are collections, and at this time the database needs to provide a database table to store.


As you can see, the mappings for the collection should be written. The mappings for the collections are roughly the same, and we use the set mappings to describe them in detail.

<set name= "Education" table= "student_education" order-by= "Education DESC" ><key column= "student_id" >< /key><element type= "string" column= "Education" ></element></set>

The Name property refers to the collection property name of the object, which refers to the name of the collection table (database table), the key child element is the column name of the foreign key of the collection, the element child element is used to hold the column information of the collection element, and the Order-by property refers to the ORDER BY clause when querying the database. This is the sort in the database. For the set sort, there is another property sort, which is sorted in memory, and the default is unsorted,sort with two other values: natural and comparatorclass, when the Sort property is used, it is required to use a sortable set, such as TreeSet and so on.

Map of List:

<list name= "Education" table= "student_education" ><key column= "student_id" ></key><list-index Column= "list_id" ></list-index><element type= "string" column= "Education" ></element></list >

The list map is similar to set, with a list-index child element, which is the index of the specified list that corresponds to which field in the collection table. Because the list is ordered, you can use the index to take a value. It is also because it is ordered, so you cannot sort by using the Sort property.

Mapping of Map:

<p><map name= "Education" table= "student_education" ></p><p> <keycolumn= "stud ent_id "></key></p><p> <map-keytype=" string "column=" Map_key "></MAP-KEY&G T;</p><p> <element type= "string" column= "Education" ></ELEMENT></P><P&G T;</map></p>

The Map-key child element is the key value of the specified map that corresponds to which field in the collection table.

In addition to the mappings for these 3 commonly used collections, Hibernate also provides mappings for two other collections: Arrays and bag. The array property is similar to list, but the length of the array is immutable; bag is similar to list, but it is unordered and repeatable. So the array map is basically the same as the list:

<array name= "Education" table= "student_education" ><key column= "student_id" ></key><list-index Column= "list_id" ></list-index><element type= "string" column= "Education" ></element></ Array>

and bag mapping is less than a list of list-index child elements:

<bag name= "Education" table= "Student_education" >                     <keycolumn= "student_id" ></key>                     < Elementtype= "string" column= "Education" ></element></bag>
Note:

When using a collection property, be sure to use an interface, not a specific implementation class. Because after the session operation, the collection becomes Hibernate's own collection implementation class.

It's better to tidy up again. About the use of the sort also order-by property:

Since both set and map are unordered, we can sort them by using the sort or order-by property. Sort is sorted in memory, and the set and map required to use it are also sortable. This sort method is not recommended, the use of more, sorting is also faster is to use the Order-by property, directly in the database sorting.


Author: Li Li
Sign: Life does not have too many illusions, but to have more action.

Set mappings for Hibernate hibernate and sort, order-by properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.