系统学习hibernate之十一:set、list、map、array集合
hibernate中的集合的使用还是比较多,跟普通的string、int等字段处理的方式不一样,下面介绍 set、list、map、array集合在hibernate中的应用。
例子:
1、 CollectionMapping的POJO类
1
import
java.util.List;
2
import
java.util.Map;
3
import
java.util.Set;
4![]()
5![]()
public
class
CollectionMapping
{
6
private int id;
7
8
private String name;
9
10
private Set setValue;
11
12
private List listValue;
13
14
private String[] arrayValue;
15
16
private Map mapValue;
17
//省略setter、getter方法
18
}
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16
17
18
2、CollectionMapping.hbm.xml映射文件
1
<?
xml version="1.0"
?>
2
<!
DOCTYPE hibernate-mapping PUBLIC
3
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
5
<
hibernate-mapping
package
="org.apple.hibernate"
>
6
<
class
name
="CollectionMapping"
table
="t_collectionMapping"
>
7
<
id
name
="id"
>
8
<
generator
class
="native"
/>
9
</
id
>
10
<
property
name
="name"
/>
11
<
set
name
="setValue"
table
="t_set_value"
>
12
<
key
column
="set_id"
/>
13
<
element
type
="string"
column
="set_value"
/>
14
</
set
>
15
<
list
name
="listValue"
table
="t_list_value"
>
16
<
key
column
="list_id"
/>
17
<
list-index
column
="list_index"
/>
18
<
element
type
="string"
column
="list_value"
/>
19
</
list
>
20
<
array
name
="arrayValue"
table
="t_array_value"
>
21
<
key
column
="array_id"
/>
22
<
list-index
column
="array_index"
/>
23
<
element
type
="string"
column
="array_value"
/>
24
</
array
>
25
<
map
name
="mapValue"
table
="t_map_value"
>
26
<
key
column
="map_id"
/>
27
<
map-key
type
="string"
column
="map_key"
/>
28
<
element
type
="string"
column
="map_value"
/>
29
</
map
>
30
</
class
>
31
</
hibernate-mapping
>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
3、利用SchemaExport生成数据库表
mysql> desc t_array_value;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| array_id | int(11) | NO | PRI | | |
| array_value | varchar(255) | YES | | NULL | |
| array_index | int(11) | NO | PRI | | |
+-------------+--------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
mysql> desc t_array_value;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| array_id | int(11) | NO | PRI | | |
| array_value | varchar(255) | YES | | NULL | |
| array_index | int(11) | NO | PRI | | |
+-------------+--------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
mysql> desc t_list_value;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| list_id | int(11) | NO | PRI | | |
| list_value | varchar(255) | YES | | NULL | |
| list_index | int(11) | NO | PRI | | |
+------------+--------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
mysql> desc t_map_value;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| map_id | int(11) | NO | PRI | | |
| map_value | varchar(255) | YES | | NULL | |
| map_key | varchar(255) | NO | PRI | | |
+-----------+--------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
mysql> desc t_set_value;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| set_id | int(11) | NO | MUL | | |
| set_value | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
2 rows in set (0.03 sec)
4、测试方法:
1
public
void
testSave1()
2![]()
{
3
Session session = null;
4
CollectionMapping c = new CollectionMapping();
5
6
c.setName("xxx");
7
8
Set setValue = new HashSet();
9
setValue.add("a");
10
setValue.add("b");
11
c.setSetValue(setValue);
12
13
List listValue = new ArrayList();
14
listValue.add("c");
15
listValue.add("d");
16
c.setListValue(listValue);
17
18![]()
String[] arrayValue = new String[]
{"e", "f"};
19
c.setArrayValue(arrayValue);
20
21
Map mapValue = new HashMap();
22
mapValue.put("k1", "v1");
23
mapValue.put("k2", "v2");
24
c.setMapValue(mapValue);
25![]()
try
{
26
session = HibernateUtil.getSession();
27
session.beginTransaction();
28
session.save(c);
29
session.getTransaction().commit();
30
31![]()
} catch (Exception e)
{
32
// TODO: handle exception
33
session.beginTransaction().rollback();
34![]()
}finally
{
35
session.close();
36
}
37
}
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

19
20
21
22
23
24
25

26
27
28
29
30
31

32
33
34

35
36
37
-------------------------------------------------------------------------------------------------
PS:本博客文章,如果没有注明是有“转”字样,属于本人原创。如果需要转载,务必注明作者和文章的详细出处地址,否则不允许转载,多谢合作!