關於List的特性,可以先看一下 ArrayList、LinkedList 這兩篇文件。
List是有序的結構,所以在儲存List集合中的物件時,要一併儲存其順序資訊,例如若您設計了以下的類別:
- User.java
package onlyfun.caterpillar;
import java.util.List;
public class User {
private Long id;
private String name;
private List items;
// 必須要有一個預設的建構方法
// 以使得Hibernate可以使用Constructor.newInstance()建立物件
public User() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public void addItem(String item) {
items.add(item);
}
public void removeItem(String item) {
items.remove(item);
}
}
在設計表格時,使用一個item表格來記錄List集合資訊,item表格必須包括索引資訊,例如您可以如下建立user與item表格:
create table item (
id bigint not null,
name varchar(255),
position integer not null,
primary key (id, position)
)
create table user (
id bigint not null auto_increment,
name varchar(255),
primary key (id)
)
alter table item
add index id (id),
add constraint id
foreign key (id)
references user (id)
id bigint not null,
name varchar(255),
position integer not null,
primary key (id, position)
)
create table user (
id bigint not null auto_increment,
name varchar(255),
primary key (id)
)
alter table item
add index id (id),
add constraint id
foreign key (id)
references user (id)
其中position欄位要用來儲存List的索引資訊,可以使用<list>標籤如下定義映射文件:
- User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="onlyfun.caterpillar.User" table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<list name="items" table="item">
<key column="id" foreign-key="id"/>
<list-index column="position"/>
<element column="name" type="string"/>
</list>
</class>
</hibernate-mapping>
假設您如下儲存物件:
User user1 = new User();
user1.setItems(new ArrayList());
user1.setName("caterpillar");
user1.addItem("DC");
user1.addItem("CF Card");
User user2 = new User();
user2.setItems(new ArrayList());
user2.setName("momor");
user2.addItem("comics");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(user1);
session.save(user2);
tx.commit();
session.close();
user1.setItems(new ArrayList());
user1.setName("caterpillar");
user1.addItem("DC");
user1.addItem("CF Card");
User user2 = new User();
user2.setItems(new ArrayList());
user2.setName("momor");
user2.addItem("comics");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(user1);
session.save(user2);
tx.commit();
session.close();
則資料庫中的儲存狀況如下:
mysql> select *
from user;
+----+--------------+
| id | name |
+----+--------------+
| 1 | caterpillar |
| 2 | momor |
+----+--------------+
2 rows in set (0.00 sec)
mysql> select * from item;
+----+------------+-----------+
| id | position | name |
+----+------------+-----------+
| 1 | 0 | DC |
| 1 | 1 | CF Card |
| 2 | 0 | comics |
+----+------------+-----------+
3 rows in set (0.00 sec)
+----+--------------+
| id | name |
+----+--------------+
| 1 | caterpillar |
| 2 | momor |
+----+--------------+
2 rows in set (0.00 sec)
mysql> select * from item;
+----+------------+-----------+
| id | position | name |
+----+------------+-----------+
| 1 | 0 | DC |
| 1 | 1 | CF Card |
| 2 | 0 | comics |
+----+------------+-----------+
3 rows in set (0.00 sec)