First, map set (set): not sorted, no duplicates.
Instance Code :
?
1 2 3 4 |
<
set name
=
"images" table
=
"IMAGES" lazy=
"true" > <
key column
=
"CUSTOMER_ID" /> <element
column
=
"FILENAME" type=
"string" not
-
null
=
"true"
/> </
set
>
|
parameter Description :
Name: Specifies the property name.
Table: Specifies the name of the table that corresponds to the property name.
Lazy: whether to delay loading.
<KEY>: Specifies the foreign key of the images.
<element>: Specifies the field that corresponds to the element in the images collection, filename.
Second, the Mapping Bag (package): Not sorted, allow repetition.
Hibernate allows you to simulate the behavior of a bag in a persisted class using a list.
Instance Code :
?
1 2 3 4 5 6 7 |
<idbag
name
=
"images" table
=
"IMAGES" lazy=
"true"
> <collection-id type=
"long" column
=
"ID"
> <generator class=
"increment"
/> </collection-id> <
key column
=
"CUSTOMER_ID" /> <element
column
=
"FILENAME" type=
"string" not
-
null
=
"true"
/> </idbag>
|
parameter Description :
<collection-id>: Used to set the ID primary key for the images table.
Third, map list (list): Sort, allow repetition.
You should first define a position field in the images table that represents the index position of each element in the collection.
Instance Code :
?
1 2 3) 4 5 |
<list
name
=
"images" table
=
"IMAGES" lazy=
"true"
> <
key column
=
"CUSTOMER_ID" /> <list-
index column
=
"POSITION" /> <element
column
=
"FILENAME" type=
"string" not
-
null
=
"true"
/> </list>
|
parameter Description :
<list-index>: Used to set which field is the basis for sorting the index.
Iv. MappingMap:
Instance Code :
?
1 2 3) 4 5 |
<map
name
=
"images" table
=
"IMAGES" lazy=
"true"
> <
key column
=
"CUSTOMER_ID" /> <map-
key column
=
"IMAGE_NAME" type=
"string"
/> <element
column
=
"FILENAME" type=
"string" not
-
null
=
"true"
/> </map>
|
parameter Description :
<map-key>: Used to set which field in the images table is the key for the map in the Java Persistence class.
V. sort the set :
Hibernate supports two sorting methods for the elements in the collection:
A, sort in the database: implemented by the order BY statement of the SQL statement.
B, sort in memory: that is, when hibernate loads the collection data in the database into memory, it uses the sort function of the Java collection to sort. Natural sort or custom sort two kinds.
The following are described in turn.
1. sort the collection in the database :
The <set>, <idbag>, and <map> elements all have the Order-by property, which can be done by setting this property by adding an order BY statement in the SELECT statement.
Example code :
?
1 2 3 4 |
<
set name
=
"images" table
=
"IMAGES" lazy=
"true" order
-
by
=
"FILENAME asc"
> <
key column
=
"CUSTOMER_ID" /> <element
column
=
"FILENAME" type=
"string" not
-
null
=
"true"
/> </
set
>
|
2. sort the collection in memory :
The <set> and <map> elements have a sort property that is set to sort the in-memory collection objects.
Instance Code : The collection type needs to implement the Java.util.SortedSet interface.
?
1 2 3 4 |
<
set name
=
"images" table
=
"IMAGES" lazy=
"true" sort=
"natural"
> <
key column
=
"CUSTOMER_ID" /> <element
column
=
"FILENAME" type=
"string" not
-
null
=
"true"
/> </
set
>
|
parameter Description :
?
1 2 |
A、natural:说明是进行自然排序。 B、客户化排序:在 sort中指定一个实现了Comparator接口的类的全限定名即可。 |
Vi. Set of mapping component types :
Instance Code :
?
1 2 3 4 5 6 7 8 9 10 |
<
set name
=
"images" table
=
"IMAGES" lazy=
"true" order
-
by
=
"IMAGE_NAME asc"
> <
key column
=
"CUSTOMER_ID" /> <composite-element class=
"mypack.Image"
> <parent
name
=
"customer" /> <property
name
=
"name" column
=
"IMAGE_NAME" not
-
null
=
"true" /> <property
name
=
"filename" column
=
"FILENAME" not
-
null
=
"true" /> <property
name
=
"sizeX" column
=
"SIZEX" not
-
null
=
"true" /> <property
name
=
"sizeY" column
=
"SIZEY" not
-
null
=
"true" /> </composite-element> </
set
>
|