關於Set的特性,您可以先參考 HashSet、TreeSet 這兩篇文件的介紹,這邊先介紹當Set中包括的物件為非實體(Entiy)時的映射方式,簡單的說,也就是所包括的物件沒有物件識別 (Identity),只是純綷的值型態(Value type)物件)。
假設您有一個User類別,當中除了名稱屬性之外,另一個就是使用者的電子郵件位址,同一個使用者可能有多個不同的郵件地址,所以在User類別中使用 Set物件來加以記錄,在這邊使用String來記錄每一筆郵件位址,為了不允許重複的郵件位址記錄,所以使用Set物件,User類別如下:
- User.java
package onlyfun.caterpillar;
import java.util.Set;
public class User {
private Long id;
private String name;
private Set emails;
// 必須要有一個預設的建構方法
// 以使得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 Set getEmails() {
return emails;
}
public void setEmails(Set emails) {
this.emails = emails;
}
public void addEmail(String email) {
this.emails.add(email);
}
public void removeEmail(String email) {
this.emails.remove(email);
}
}
要映射Set集合,您可以使用另一個表格來儲存Set集合中的資料,例如您可以分別建立user與email表格:
create table email (
id bigint not null,
address varchar(255)
)
create table user (
id bigint not null auto_increment,
name varchar(255),
primary key (id)
)
alter table email
add index id (id),
add constraint id
foreign key (id)
references user (id)
id bigint not null,
address varchar(255)
)
create table user (
id bigint not null auto_increment,
name varchar(255),
primary key (id)
)
alter table email
add index id (id),
add constraint id
foreign key (id)
references user (id)
接著定義映射文件,使用<set>標籤來定義Set映射:
- 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"/>
<set name="emails" table="email">
<key column="id" foreign-key="id"/>
<element type="string" column="address"/>
</set>
</class>
</hibernate-mapping>
假設您如下儲存物件:
User user1 = new User();
user1.setEmails(new HashSet());
user1.setName("caterpillar");
user1.addEmail("caterpillar.onlyfun@gmail.com");
user1.addEmail("caterpillar.onlyfun@yahoo.com");
User user2 = new User();
user2.setEmails(new HashSet());
user2.setName("momor");
user2.addEmail("momor@gmail.com");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(user1);
session.save(user2);
tx.commit();
session.close();
user1.setEmails(new HashSet());
user1.setName("caterpillar");
user1.addEmail("caterpillar.onlyfun@gmail.com");
user1.addEmail("caterpillar.onlyfun@yahoo.com");
User user2 = new User();
user2.setEmails(new HashSet());
user2.setName("momor");
user2.addEmail("momor@gmail.com");
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 email;
+----+-------------------------------------------+
| id | address |
+----+-------------------------------------------+
| 1 | caterpillar.onlyfun@yahoo.com |
| 1 | caterpillar.onlyfun@gmail.com |
| 2 | momor@gmail.com |
+----+-------------------------------------------+
3 rows in set (0.00 sec)
+----+----------------+
| id | name |
+----+----------------+
| 1 | caterpillar |
| 2 | momor |
+----+----------------+
2 rows in set (0.00 sec)
mysql> select * from email;
+----+-------------------------------------------+
| id | address |
+----+-------------------------------------------+
| 1 | caterpillar.onlyfun@yahoo.com |
| 1 | caterpillar.onlyfun@gmail.com |
| 2 | momor@gmail.com |
+----+-------------------------------------------+
3 rows in set (0.00 sec)