在上一篇的文章中我们写到了一对多的关联映射,它是多对多关联映射的基础。多对多的关联映射也是我们最常见的一种关联映射。
多对多关联映射的原理是通过第三张表来维护两个对象多对多的关联关系,即将两张表的主键放到第三张表中做关联。
上一篇文章中用到了User和Group两个实体类,一个Group对应多个User,现在我们假设一个User也可以对应多个Group,来看一下多对多的关联映射情况。
多对多的关联映射也是分为单向和双向的(其和一对多的关联映射及其相似,只不过将引入的one-to-many替换成了many-to-many);
其中单向多对多关联映射(即只能从一端查看另一端,不能反过来,这里例子是只能从 Group查看User):
Group.java
package com.suo.bo;
import java.util.Set;
/**
* Group entity. @author MyEclipse Persistence Tools
*/
public class Group implements java.io.Serializable {
// Fields
private Integer id;
private String name;
//加入对User的引用(多对多,使用set)
private Set users;
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
}
Grouphbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.suo.bo.Group" table="group" catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<!-- Group类中的users属性,表达本对象和User的多对多关系 -->
<!-- table表写的是关联多对多关系引入的第三张表名称 -->
<set name="users" table="t_group_user">
<!-- key column=第三张 表中指向Group的字段,下面一行是指向User的字段-->
<key column="groupid"></key>
<many-to-many class="com.suo.bo.User" column="userid"></many-to-many>
</set>
</class>
</hibernate-mapping>
User.java
package com.suo.bo;
/**
* User entity. @author MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer id;
private String name;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.suo.bo.User" table="user" catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
</class>
</hibernate-mapping>
即在单向的基础上在User实体类中加入对Group的引用;
User.java将变成如下代码:
package com.suo.bo;
import java.util.Set;
/**
* User entity. @author MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer id;
private String name;
//加入对Group的引用,因为是多对多所以记得用set
private Set groups;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getGroups() {
return groups;
}
public void setGroups(Set groups) {
this.groups = groups;
}
}
然后在User.hbm.xml中加入many-to-many标签:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.suo.bo.User" table="user" catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<set name="groups" table="t_group-user">
<key column="userid"></key>
<many-to-many class="com.suo.bo.Group" column="groupid"></many-to-many>
</set>
</class>
</hibernate-mapping>
Group.java与单向时一致:
package com.suo.bo;
import java.util.Set;
/**
* Group entity. @author MyEclipse Persistence Tools
*/
public class Group implements java.io.Serializable {
// Fields
private Integer id;
private String name;
//加入对User的引用(多对多,使用set)
private Set users;
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
}
Group.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.suo.bo.Group" table="group" catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<!-- Group类中的users属性,表达本对象和User的多对多关系 -->
<!-- table表写的是关联多对多关系引入的第三张表名称 -->
<set name="users" table="t_group_user">
<!-- key column=第三张 表中指向Group的字段,下面一行是指向User的字段-->
<key column="groupid"></key>
<many-to-many class="com.suo.bo.User" column="userid"></many-to-many>
</set>
</class>
</hibernate-mapping>