`

hibernate入门三:一对多关联关系映射

阅读更多
  实体之间的一对多关系是很常见的一种关联关系,例如:一个人有多套房子,那就有多个地址,一个人在淘宝分不同时段买了多个东西,那就有多个订单;对于在数据库来说,那就是外键的约束关系,多的一方有个字段是外键,指向一的一方。在hibernate中,一对多关联关系映射分为2种,需分情况使用。一种是单向关联关系映射,一种是双向关系映射,下面以2张表说明,表中外键约束通过hibernate维护。下面是表间关系



其去掉foreign key 的SQL为:
CREATE TABLE `ADDRESS` (
  `ID` int(4) NOT NULL auto_increment,
  `CITY` varchar(40) default NULL,
  `STREET` varchar(100) default NULL,
  `PERSON_ID` int(4) NOT NULL default '0',
  PRIMARY KEY  (`ID`)

) ENGINE=MyISAM DEFAULT CHARSET=gb2312;
CREATE TABLE `PERSON` (
  `ID` int(4) NOT NULL auto_increment,
  `NAME` char(20) default NULL,
  `PHONE` char(20) default NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;

一对多单向关联关系和一对多双向关联关系数据库表相同,不同的只是映射的JAVA类及映射文件的配置。DAO代码和测试代码省略。
一对多单向关联关系:
实体类:
package com.onetomanysingle.model;

/**
 * Address entity
 */

public class Address implements java.io.Serializable {

	// Fields

	private Integer id;
	private String city;
	private String street;
	private Person person;

	// Constructors

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

	/** minimal constructor */
	public Address(Person person) {
		this.person = person;
	}

	/** full constructor */
	public Address(String city, String street, Person person) {
		this.city = city;
		this.street = street;
		this.person = person;
	}

	// Property accessors

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

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

	public String getCity() {
		return this.city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getStreet() {
		return this.street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public Person getPerson() {
		return this.person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

}

package com.onetomanysingle.model;

/**
 * Person entity
 */

public class Person implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;
	private String phone;

	// Constructors

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

	/** full constructor */
	public Person(String name, String phone) {
		this.name = name;
		this.phone = phone;
	}

	// 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 String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

}

映射文件:
Address.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.onetomanysingle.model.Address" table="address" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="city" type="java.lang.String">
            <column name="CITY" length="40" />
        </property>
        <property name="street" type="java.lang.String">
            <column name="STREET" length="100" />
        </property>
        <!-- 映射person到address的一对多单向关联即address到person的多对一单向关联
        具体意义是:通过查询地址可以查询到此地址所住的人(一个)。 -->
        <many-to-one name="person"
                     column="PERSON_ID"
                     class="com.onetomanysingle.model.Person"
                     lazy="false"
                     not-null="true"/>
                     <!-- 以上分别说明映射字段在类中的属性,表中的字段,对应的类,立即加载,非空 -->
    </class>
</hibernate-mapping>

Person.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.onetomanysingle.model.Person" table="person" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="20" />
        </property>
        <property name="phone" type="java.lang.String">
            <column name="PHONE" length="20" />
        </property>
    </class>
</hibernate-mapping>

一对多单向关联关系,可以从多的一方查询到一的一方,但从一的一方查询不到多的一方。

一对多双向关联关系映射:
实体类:
package com.onetomanydouble.model;

/**
 * Address entity
 */

public class Address implements java.io.Serializable {

	// Fields

	private Integer id;
	private String city;
	private String street;
	private Person person;

	// Constructors
	public Address() {
		super();
	}

	public Address(Integer id, String city, String street, Person person) {
		super();
		this.id = id;
		this.city = city;
		this.street = street;
		this.person = person;
	}

	// Property accessors

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

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

	public String getCity() {
		return this.city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getStreet() {
		return this.street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

}

package com.onetomanydouble.model;

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

/**
 * Person entity
 */

public class Person implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;
	private String phone;
	private Set address = new HashSet();

	// Constructors

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

	public Person(Integer id, String name, String phone, Set address) {
		super();
		this.id = id;
		this.name = name;
		this.phone = phone;
		this.address = address;
	}

	// 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 String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Set getAddress() {
		return address;
	}

	public void setAddress(Set address) {
		this.address = address;
	}

}

映射文件:
Address.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.onetomanydouble.model.Address" table="address" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="city" type="java.lang.String">
            <column name="CITY" length="40" />
        </property>
        <property name="street" type="java.lang.String">
            <column name="STREET" length="100" />
        </property>
       <!-- 多对一映射 -->
       <many-to-one name="person" column="PERSON_ID"
       class="com.onetomanydouble.model.Person"
       lazy="false"
       not-null="true"/>
    </class>
</hibernate-mapping>

Person.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.onetomanydouble.model.Person" table="person" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="20" />
        </property>
        <property name="phone" type="java.lang.String">
            <column name="PHONE" length="20" />
        </property>
        <!-- 一对多映射 inverse说明这是主控方,true由被控方(多的一方)维护外键关系,一般来说,都应该由多的一方来维护 -->
        <set name="address" 
        cascade="all"
        inverse="true"
        lazy="false">
        <key column="person_id"/>
        <one-to-many class="com.onetomanydouble.model.Address"/>
        </set>
    </class>
</hibernate-mapping>

一对多双向关联关系,双方均可查询到对方。

从上面可以看出,一对多的单向和双向,只不过是在类中是否有对方作为属性,一的一方有个多的一方的set集合,多的一方有个一的一方做为属性。在配置文件中,单双向多的一方的配置均相同,只有一的一方不同。
  • 大小: 13.8 KB
分享到:
评论

相关推荐

    Hibernate 对象关系映射

    本人在厦门邦初培训时候用的ppt 快速入门文档 内容: 建立一对多的关联关系域模型 建立一对多的参照关系的模型 映射一对多关联关系 通过hibernateAPI级联操作关联对象 其它关系的映射:一对一,多对多

    Hibernate3.1_学习源码

    案例目录: 01 01Hibernate_Handwork : 手工配置使用Hibernate,其中详细标了Hibernate进行持久化的一些过程,因为是Hibernate的入门实例,所以注释很详细,其中有... 一对一、多对一、一对多、多对多等几种情况。

    传智播客李勇hibernate源码1-20课

    传智播客李勇hibernate源码1-20课,目录如下:01_hibernate介绍... 17_一对多关联关系的映射与原理分析 18_一对一主键关联关系的映射与原理分析 19_一对一主键关联关系的检索 20_一对一外键关联关系的映射与原理分析

    hibernate入门与概述

    hibernate入门与概述。介绍对象-关系 映射基础,映射对象标识符,映射一对多关联关系,操纵持久化对象,hibernate的检索策略。

    Hibernate入门教程学习笔记

    这是一个Hibernate入门学习的不错资源,主要内容包括Hibernate主配置文件的讲解、映射普通属性、映射主键、映射集合、映射关联关系(一对多多对一、多对多、一对一)、HQL查询等内容,最后给出一个综合练习案例

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate的ppt

    hibernate入门;java对象持久化;对象-关系映射基础;...映射一对多关联关系;映射一对一关联关系;映射多对多关联关系;操纵持久化对象;映射组成关系;hibernate映射类型;hibernate的检索策略等等。

    Hibernate+中文文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate教程

    7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    HibernateAPI中文版.chm

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    hibernate3.2中文文档(chm格式)

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate中文详细学习文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate 中文 html 帮助文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    hibernate 体系结构与配置 参考文档(html)

    一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...

    Hibernate基础入门教程

    开篇:基本映射 1对1(主键关联) 1对1(唯一外键关联) 多对一 一对多 多对多 其次:HQL等查询

    hibernate3.04中文文档.chm

    7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    Hibernate参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    hibernate 框架详解

    一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    Hibernate3的帮助文档

    7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    Hibernate学习总结

    学习成长路,Hibernate总结: 1.Hibernate入门优缺点、 ...5.映射一对多关联关系、 6.深入session、 7.懒加载、 8.Hibernate检索策略(fetch抓取策略)、 9.二级缓存、 10.Hbernate的检索方式(HQL语句)

Global site tag (gtag.js) - Google Analytics