Hibernate中的几种映射关系----------多对多关联映射

        在上一篇的文章中我们写到了一对多的关联映射,它是多对多关联映射的基础。多对多的关联映射也是我们最常见的一种关联映射。

       多对多关联映射的原理是通过第三张表来维护两个对象多对多的关联关系,即将两张表的主键放到第三张表中做关联。

      上一篇文章中用到了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>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值