在资料库表格上要进行多对多对应,可以藉由一个中介表格来完成,也就是藉由多对一、一对多来完成多对多关联。
多对多由于使用了中介表格,在查询效率不彰,且在程式的物件模式上,多对多会使得物件与物件之间彼此依赖,并不是一个很好的设计方式,在设计上应避免使用多对多关系。
如果一定要使用多对多关系的话,在表格上先如下建立:
create table server (
id integer not null auto_increment,
address varchar(255),
primary key (id)
)
create table user (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table user_server (
user_id integer not null,
server_id integer not null,
primary key (user_id, server_id)
)
先设计User类别如下:package onlyfun.caterpillar;
import java.util.Set;
public class User {
private Integer id;
private String name;
private Set servers;
public User() {}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getServers() {
return servers;
}
public void setServers(Set servers) {
this.servers = servers;
}
}
再来设计Server类别如下:
package onlyfun.caterpillar;
import java.util.Set;
public class Server {
private Integer id;
private String address;
private Set users;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
}
在映射文件上,使用<many-to-many>标签来完成映射关系:
<?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="servers"
table="user_server"
cascade="save-update">
<key column="user_id"/>
<many-to-many class="onlyfun.caterpillar.Server"
column="server_id"/>
</set>
</class>
</hibernate-mapping>
注意到cascade是设定为save-update,因为在多对多的关系中,很少因为删除其中之一,而所关联的实体都要一并删除的,所以设定save- update,表示在save或update时,一并对关联的物件进行对应的save或update。
Server.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.Server" table="server">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="address"/>
<set name="users"
table="user_server"
inverse="true"
cascade="save-update">
<key column="server_id"/>
<many-to-many class="onlyfun.caterpillar.User"
column="user_id"/>
</set>
</class>
</hibernate-mapping>
一个储存时的例子如下:
Server server1 = new Server();
server1.setAddress("PC-219");
server1.setUsers(new HashSet());
Server server2 = new Server();
server2.setAddress("PC-220");
server2.setUsers(new HashSet());
Server server3 = new Server();
server3.setAddress("PC-221");
server3.setUsers(new HashSet());
User user1 = new User();
user1.setName("caterpillar");
user1.setServers(new HashSet());
User user2 = new User();
user2.setName("momor");
user2.setServers(new HashSet());
// 多对多,互相参考
user1.getServers().add(server1);
user1.getServers().add(server2);
user1.getServers().add(server3);
server1.getUsers().add(user1);
server2.getUsers().add(user1);
server3.getUsers().add(user1);
user2.getServers().add(server1);
user2.getServers().add(server3);
server1.getUsers().add(user2);
server3.getUsers().add(user2);
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx= session.beginTransaction();
session.save(user1);
session.save(user2);
tx.commit();
session.close();