hibernate入门四:多对多关联关系映射

  hibernate中多对多关联关系映射和一对多关联关系映射一样,分为单双向。从基本形式上,可看作是一对多,多对一的结合,其实也就是2个一对多或者多对一。从数据库上来说,为了满足范式的规约,多对多经常采用中间表的方式经行关联。关于hibernate的多对多而言,我们一般无法操纵中间表,也就是一般来说,我们的中间表多采用2个字段,联合主键的形式。如果想要操作中间表,比如在业务需求中中间表必须带有其他字段,就需要对中间表经行实体映射,基本的解决思路是将多对多,拆解为2个一对多(one-to-many)——两个主表对中间表,以及中间表对主表的2个多对一(many-to-one)。以下例子是引用java web整合开发中的,是很好的例子,只是进行了必要的注释。
   首先还是数据库关系图:

hibernate入门四:多对多关联关系映射
  
多对多单向关联关系
2个实体类:
package com.manytomanysingle.model;

/**
 * Items entity
 */

public class Items implements java.io.Serializable {

	// Fields

	private Integer id;
	private String itemno;
	private String itemname;

	// Constructors

	/** default constructor */
	public Items() {
	}

	/** full constructor */
	public Items(String itemno, String itemname) {
		this.itemno = itemno;
		this.itemname = itemname;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getItemno() {
		return this.itemno;
	}

	public void setItemno(String itemno) {
		this.itemno = itemno;
	}

	public String getItemname() {
		return this.itemname;
	}

	public void setItemname(String itemname) {
		this.itemname = itemname;
	}

}

package com.manytomanysingle.model;

import java.util.HashSet;
import java.util.Set;

/**
 * Orders entity.
 */

public class Orders implements java.io.Serializable {

	// Fields

	private Integer id;
	private String orderno;
	private Double money;
	private Set items = new HashSet();

	// Constructors

	public Orders() {
		super();
	}

	public Orders(Integer id, String orderno, Double money, Set items) {
		super();
		this.id = id;
		this.orderno = orderno;
		this.money = money;
		this.items = items;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getOrderno() {
		return this.orderno;
	}

	public void setOrderno(String orderno) {
		this.orderno = orderno;
	}

	public Double getMoney() {
		return this.money;
	}

	public void setMoney(Double money) {
		this.money = money;
	}

	public Set getItems() {
		return items;
	}

	public void setItems(Set items) {
		this.items = items;
	}

}

映射文件:
Items.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="com.manytomanysingle.model.Items" table="items" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="itemno" type="java.lang.String">
            <column name="ITEMNO" length="20" />
        </property>
        <property name="itemname" type="java.lang.String">
            <column name="ITEMNAME" length="60" />
        </property>
    </class>
</hibernate-mapping>

Orders.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="com.manytomanysingle.model.Orders" table="orders" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="orderno" type="java.lang.String">
            <column name="ORDERNO" length="20" />
        </property>
        <property name="money" type="java.lang.Double">
            <column name="MONEY" precision="10" />
        </property>
        <!-- 映射多对多关联单向关联,orders到items,查询orders将查询到items -->
        <set name="items" table="selecteditems" lazy="true" cascade="save-update">
        <key column="orderid"/>
        <many-to-many class="com.orm.items" column="itemid"/>
        </set>
    </class>
</hibernate-mapping>


多对多双向关联关系
实体类:
package com.manytomanydouble.model;

import java.util.HashSet;
import java.util.Set;

/**
 * Items entity.
 */

public class Items implements java.io.Serializable {

	// Fields

	private Integer id;
	private String itemno;
	private String itemname;
	private Set orders = new HashSet();

	// Constructors

	/** default constructor */
	public Items() {
	}

	public Items(Integer id, String itemno, String itemname, Set orders) {
		super();
		this.id = id;
		this.itemno = itemno;
		this.itemname = itemname;
		this.orders = orders;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getItemno() {
		return this.itemno;
	}

	public void setItemno(String itemno) {
		this.itemno = itemno;
	}

	public String getItemname() {
		return this.itemname;
	}

	public void setItemname(String itemname) {
		this.itemname = itemname;
	}

	public Set getOrders() {
		return orders;
	}

	public void setOrders(Set orders) {
		this.orders = orders;
	}

}

package com.manytomanydouble.model;

import java.util.HashSet;
import java.util.Set;

/**
 *Orders entity.
 */
public class Orders implements java.io.Serializable {

	// Fields

	private Integer id;
	private String orderno;
	private Double money;
    private Set items=new HashSet();
    
	public Orders() {
		super();
	}
	
	public Orders(Integer id, String orderno, Double money, Set items) {
		super();
		this.id = id;
		this.orderno = orderno;
		this.money = money;
		this.items = items;
	}


	// Property accessors
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getOrderno() {
		return this.orderno;
	}

	public void setOrderno(String orderno) {
		this.orderno = orderno;
	}

	public Double getMoney() {
		return this.money;
	}

	public void setMoney(Double money) {
		this.money = money;
	}

	public Set getItems() {
		return items;
	}

	public void setItems(Set items) {
		this.items = items;
	}

}


映射文件:
Items.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="com.manytomanydouble.model.Items" table="items" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="itemno" type="java.lang.String">
            <column name="ITEMNO" length="20" />
        </property>
        <property name="itemname" type="java.lang.String">
            <column name="ITEMNAME" length="60" />
        </property>
        <!-- 映射多对多 ,这是被控方,orders为主控方,外键关系由orders维护-->
        <set name="orders" table="selecteditems" lazy="true"
        inverse="true" cascade="save-update">
        <!-- 指明Iterms中主键在其他表中作为外键使用时的字段名 -->
        <key column="itemid"/>
        <!-- 指明集合对应的类,集合中全是此类的对象,以及指明了Items参照这个类使用的外键名 -->
        <many-to-many class="com.manytomanydouble.model.Orders" column="orderid"/>
        </set>
    </class>
</hibernate-mapping>

Orders.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="com.manytomanydouble.model.Orders" table="orders" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="orderno" type="java.lang.String">
            <column name="ORDERNO" length="20" />
        </property>
        <property name="money" type="java.lang.Double">
            <column name="MONEY" precision="10" />
        </property>
        <!-- 映射多对多 -->
        <set name="items" table="selecteditems" lazy="true" cascade="save-update">
        <!-- 主键在其他表中作为外键的字段名 -->
        <key column="orderid"/>
        <!-- 集合中存放的对象对应的类,Orders的关于Iterms表的外键名称 -->
        <many-to-many class="com.manytomanydouble.model.Items" column="itemid"/>
        </set>
    </class>
</hibernate-mapping>

你可能感兴趣的:(java,Hibernate)