Common set Set, List, Map, I believe we are very familiar with the interview will often ask. Both sets and lists inherit the collection interface, and set is unordered, and cannot store the same elements, while lsit is stored sequentially and can store duplicate elements. Map does not inherit collection, map provides mapping of key to value, map is not 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, you need to add database tables to complete the mapping to save them to the database. For example, a student may have more than one educational background, and this time the attributes of the persisted class are set, and the database will need to provide a database table to store.
As you can see from the above diagram, the mapping of the collection should be written. The mappings of the set are roughly the same, we use the mapping of set to specify.
<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 (the database table), which refers to the column name of the foreign key of the collection, the information that the element child element uses to hold the column of the collection element, and the Order-by property is the specified ORDER BY clause when This is the sort in the database. For the order of set, there is another property sort, which is sorted in memory, by default Unsorted,sort and two other values: natural and comparatorclass, when the sort attribute is used, a set that can be sorted is required. 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 map of the list is similar to set, with a list-index child element that specifies which field in the collection table the index of the list corresponds to. Because the list is ordered, you can use the index to fetch the value. Also because it is ordered, you cannot use the Sort property for sorting.
Map Mapping:
<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 field in the collection table where the key value of the specified map corresponds.
In addition to the mappings of these 3 commonly used collections, Hibernate also provides mappings for two other sets: Arrays and bag. The array property is similar to the list, but the length of the array is immutable; bag is also similar to list, but it is unordered and repeatable. So the mapping of the array 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>
The bag mapping is a list-index child element less than the list:
<bag name= "Education" table= "Student_education" >
<keycolumn= "student_id" ></key>
< Elementtype= "string" column= "education" ></element>
</bag>
Attention:
When using collection properties, be sure to use an interface instead of declaring it as a concrete implementation class. Because after the session, the collection becomes Hibernate's own collection implementation class.
And finally, the use of sort also order-by properties:
Since set and map are unordered, we can sort them by using the sort or order-by properties. Sort is sorted in memory, and the set and map required to be used are also sortable. This sort of sorting method is not recommended, use more, sort also relatively fast is to use the order-by attribute, sort directly in the database.
Author: Li Li
Sign: Life should not have too many fantasies, but to have more action.