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

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

[img]http://dl2.iteye.com/upload/attachment/0089/0759/64f5d3f1-ad43-31a6-b011-4cbe200aa871.jpg[/img]

其去掉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集合,多的一方有个一的一方做为属性。在配置文件中,单双向多的一方的配置均相同,只有一的一方不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值