關於Map的特性,請參考 HashMap、TreeMap 這兩篇文件。
Map的特性是key/value對,容器中的每一個物件都有一個key與之對應,所以將Map集合的資料儲存至資料庫時,必須一同儲存它的key資訊。
假設您設計了以下的類別:
- User.java
package onlyfun.caterpillar;
import java.util.Map;
public class User {
private Long id;
private String name;
private Map 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 Map getItems() {
return items;
}
public void setItems(Map items) {
this.items = items;
}
public void addItem(String name, String description) {
items.put(name, description);
}
public void removeItem(String name) {
items.remove(name);
}
}
您可以建立以下的表格來分別儲存user資訊與當中的items資訊:
create table item (
id bigint not null,
description varchar(255),
name varchar(255) not null,
primary key (id, name)
)
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,
description varchar(255),
name varchar(255) not null,
primary key (id, name)
)
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)
在映射文件方面,使用<map>標籤來定義Map的映射,如下:
- 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"/>
<map name="items" table="item">
<key column="id" foreign-key="id"/>
<map-key column="name" type="string"/>
<element column="description" type="string"/>
</map>
</class>
</hibernate-mapping>
假設您使用以下的程式片段來儲存User實例:
User
user1 = new User();
user1.setItems(new HashMap());
user1.setName("caterpillar");
user1.addItem("Book", "Java Gossip");
user1.addItem("DC", "Caxxx A80");
User user2 = new User();
user2.setItems(new HashMap());
user2.setName("momor");
user2.addItem("Doll", "Snoppy world");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(user2);
tx.commit();
session.close();
user1.setItems(new HashMap());
user1.setName("caterpillar");
user1.addItem("Book", "Java Gossip");
user1.addItem("DC", "Caxxx A80");
User user2 = new User();
user2.setItems(new HashMap());
user2.setName("momor");
user2.addItem("Doll", "Snoppy world");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
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 | name | description |
+----+--------+-------------------+
| 1 | DC | Caxxx A80 |
| 1 | Book | Java Gossip |
| 2 | Doll | Snoppy world |
+----+-------+-------------------+
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 | name | description |
+----+--------+-------------------+
| 1 | DC | Caxxx A80 |
| 1 | Book | Java Gossip |
| 2 | Doll | Snoppy world |
+----+-------+-------------------+
3 rows in set (0.00 sec)