入门 19 - 一对多实体映像
在前一个主题中,User对Room是多对一,反过来看,Room对User是一对多,一个Room可以给多个User住宿使用,我们的User类别这次设计如下:
User.java
package onlyfun.caterpillar;
public class User {
private long id;
private String name;
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;
}
}
这次不在User中包括Room属性,我们在Room类别中使用Set来储存User,表示一个Room中所住宿的使用者:
Room.java
package onlyfun.caterpillar;
import java.util.*;
public class Room {
private long id;
private String address;
private Set users = new HashSet();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
}
关于User的映射文件User.hbm.xml如下:
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="onlyfun.caterpillar.User" table="USER">
<id name="id" column="USER_ID">
<generator class="increment"/>
</id>
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
</class>
</hibernate-mapping>
而Room的映射文件Room.hbm.xml如下,我们使用<set>来作集合映像,而在当中使用<one-to-many>来指定Room对User的一对多关系:
Room.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="onlyfun.caterpillar.Room" table="ROOM">
<id name="id" column="ROOM_ID">
<generator class="increment"/>
</id>
<property name="address" type="string"/>
<set name="users" table="USER">
<key column="ROOM_ID"/>
<one-to-many class="onlyfun.caterpillar.User"/>
</set>
</class>
</hibernate-mapping>
我们使用下面这个程序来测试数据的储存:
HibernateTest.java
import onlyfun.caterpillar.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateTest {
public static void main(String[] args) throws HibernateException {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Room room = new Room();
room.setAddress("NTU-M8-419");
User user1 = new User();
user1.setName("bush");
User user2 = new User();
user2.setName("caterpillar");
room.getUsers().add(user1);
room.getUsers().add(user2);
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(user1);
session.save(user2);
session.save(room);
tx.commit();
session.close();
sessionFactory.close();
}
}
看看数据库中实际储存的内容:
mysql> select * from USER;
+---------+-------------+---------+
| USER_ID | NAME | ROOM_ID |
+---------+-------------+---------+
| 1 | bush | 1 |
| 2 | caterpillar | 1 |
+---------+-------------+---------+
2 rows in set (0.00 sec)
mysql> select * from ROOM;
+---------+------------+
| ROOM_ID | address |
+---------+------------+
| 1 | NTU-M8-419 |
+---------+------------+
1 row in set (0.00 sec)
在这边我们先引出两个主题,第一是每次我们都用save()储存所有的暂存对象吗?事实上我们只要透用Hibernate的「可达性」,上面的三行 save(),就可以简化只save(room),而其所含的user1、user2也会自动储存。第二是User与Room之间的关系我们到现在都只设计单向的,事实上我们会可以设计它们之间的双向关联。
这两个主题都值得独立一个探讨,之后将会介绍。