一, 多对一的单向关联
实体之间的多对一单向关联关系是比较常见的一种关联关系,比如订单与客户,购物车与顾客之间的关系都属于多对一关联关系,这种多对一的关联关系在关系型数据库中通过外键参照是很容易实现的。
order(表) ----n--------------------------1-> customer
id id
orderno cname
money bank
customer_id int fk phone
customer_id references customer(id)
订单与客户表是多对一的关联。
对象关系图(单向关联)
n 1
Orders.java ---------------------------------------> Customer
id Integer id Integer
orderno String cname String
money Double bank String
customer Customer phone String
1. java类
package com.xiu.hibernate.bean;
import java.io.Serializable;
public class Customer implements Serializable {
private Integer id;
private String cname;
private String bank;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getBank() {
return bank;
}
public void setBank(String bank) {
this.bank = bank;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
package com.xiu.hibernate.bean;
import java.io.Serializable;
public class Orders implements Serializable {
private Integer id;
private String orderno;
private Double mony;
private Customer customer;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOrderno() {
return orderno;
}
public void setOrderno(String orderno) {
this.orderno = orderno;
}
public Double getMony() {
return mony;
}
public void setMony(Double mony) {
this.mony = mony;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
2. 配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xiu.hibernate.bean">
<class name="Customer" table="customer">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>
<property name="cname" type="string" column="cname"></property>
<property name="phone" type="string" column="phone"></property>
<property name="bank" type="string" column="bank"></property>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xiu.hibernate.bean">
<class name="Orders" table="orders">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>
<property name="orderno" type="string" column="orderno"></property>
<property name="mony" type="double" column="mony"></property>
<many-to-one name="customer"
class="com.xiu.hibernate.bean.Customer" column="customer_id"
lazy="false" not-null="true">
</many-to-one>
</class>
</hibernate-mapping>
3. 测试类
package com.xiu.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.xiu.hibernate.bean.Customer;
import com.xiu.hibernate.bean.Orders;
public class TestManyToOne {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateSessionFactory
.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.begin();
Customer customer = new Customer();
customer.setBank("445");
customer.setCname("小朱");
customer.setPhone("119");
Orders order = new Orders();
order.setCustomer(customer);
order.setMony(234.45);
order.setOrderno("4545");
Orders order2 = new Orders();
order2.setCustomer(customer);
order2.setMony(23234.45);
order2.setOrderno("45232323");
session.save(customer);
session.save(order);
session.save(order2);
tx.commit();
}
}
4.运行结果
Hibernate:
select
max(id)
from
customer
Hibernate:
select
max(id)
from
orders
Hibernate:
insert
into
customer
(cname, phone, bank, id)
values
(?, ?, ?, ?)
Hibernate:
insert
into
orders
(orderno, mony, customer_id, id)
values
(?, ?, ?, ?)
Hibernate:
insert
into
orders
(orderno, mony, customer_id, id)
values
(?, ?, ?, ?)
二 , 多对一的双向关联
还是以上面的实例为主, 其他的没有改变,就是在Customer类中增加Orders的引用,最终的结果为
Customer类
id
cname
bank
phone
orders Set
在Customer.hbm.xml文件中增加以下内容
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xiu.hibernate.bean">
<class name="Customer" table="customer">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>
<property name="cname" type="string" column="cname"></property>
<property name="phone" type="string" column="phone"></property>
<property name="bank" type="string" column="bank"></property>
<set name="orders" cascade="all" inverse="true" lazy="false">
<!-- cascade表示级联,他有以下取值
1. all: 表示所有操作均在关联层级上进行连锁操作
2. save-upate 表示只有save与update操作进行连锁操作
3. delete 表示只有delete操作进行连锁操作
4. all-delete-orphan
inverse 若为false表示主控制方负责关联关系的维护,若为true表示
有被控制方来维护关系,默认是false
-->
<key column="customer_id"></key>
<one-to-many class="com.xiu.hibernate.bean.Orders" />
</set>
</class>
</hibernate-mapping>
三,一对多双向自身关联
在电子商务中,通常用一张表来存放所有的商品分类,分类之间的层级关系是典型的自身一对多双向关联关系,也同样通过外键参照来实现,从某种意义上讲,一对多双向自身关联关系只是一种一对多双向关联关系的一种特殊情况,因为,参与关联的双方都是同一个持久化类,也即是自身。
商品分类表(gooodscate)
id int pk
parent_id int fk references gooodscate(id)
cate_no varchar
cate_name varchar
对应的java类
Goodscate
id Integer
parent Goodscate
childs Set
cateNo String
cateName String
1 n
parent -------------------->childs
Goodscate 类
package com.xiu.hibernate.relation;
import java.io.Serializable;
import java.util.Set;
/**
* 一对多 自身关联的关系,只有一张表,
* 在一张表中表现出 一对多的关系
*
* 思路如下:在一张表中新建一个字段为外键,
* 该外键参照的是主键,
* java对象中的思路:只需一个java对象,必须包含以下:
* 必须有parent和childern
* parent和childern是一对多的关系,
*
*
* @author zhuguanghe
*
*/
public class Goodscate implements Serializable {
private Integer id;
private Goodscate parent;
private Set<Goodscate> children;
private String cateNo;
private String cateName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Goodscate getParent() {
return parent;
}
public void setParent(Goodscate parent) {
this.parent = parent;
}
public Set<Goodscate> getChildren() {
return children;
}
public void setChildren(Set<Goodscate> children) {
this.children = children;
}
public String getCateNo() {
return cateNo;
}
public void setCateNo(String cateNo) {
this.cateNo = cateNo;
}
public String getCateName() {
return cateName;
}
public void setCateName(String cateName) {
this.cateName = cateName;
}
}
Goodscate.hbm.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xiu.hibernate.relation">
<class name="Goodscate" table="goodscate">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>
<!-- 映射子类别到父类别的多对一关联 -->
<many-to-one name="parent" column="parent_id"
class="com.xiu.hibernate.relation.Goodscate" lazy="false">
</many-to-one>
<!-- 映射父类别到子类别的一对多关联 -->
<set name="children" cascade="all" lazy="false"
inverse="true">
<key column="parent_id"></key>
<one-to-many class="com.xiu.hibernate.relation.Goodscate" />
</set>
<property name="cateNo" type="string" column="cate_no"></property>
<property name="cateName" type="string" column="cate_name"></property>
</class>
</hibernate-mapping>
测试类
package com.xiu.hibernate.relation;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.xiu.hibernate.test.HibernateSessionFactory;
public class TestGoodscate {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateSessionFactory
.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.begin();
Goodscate gs = new Goodscate();
gs.setCateName("商品名称");
gs.setCateNo("商品编号");
Goodscate children = new Goodscate();
children.setCateName("商品名称children");
children.setCateNo("商品编号children");
children.setParent(gs);
session.save(gs);
session.save(children);
tx.commit();
Goodscate gst=(Goodscate) session.load(Goodscate.class, 2);
System.out.println(gst.getParent().getCateName());
}
}
运行结果
Hibernate:
select
max(id)
from
goodscate
Hibernate:
insert
into
goodscate
(parent_id, cate_no, cate_name, id)
values
(?, ?, ?, ?)
Hibernate:
insert
into
goodscate
(parent_id, cate_no, cate_name, id)
values
(?, ?, ?, ?)
Hibernate:
select
goodscate0_.id as id4_0_,
goodscate0_.parent_id as parent2_4_0_,
goodscate0_.cate_no as cate3_4_0_,
goodscate0_.cate_name as cate4_4_0_
from
goodscate goodscate0_
where
goodscate0_.id=?
Hibernate:
select
goodscate0_.id as id4_0_,
goodscate0_.parent_id as parent2_4_0_,
goodscate0_.cate_no as cate3_4_0_,
goodscate0_.cate_name as cate4_4_0_
from
goodscate goodscate0_
where
goodscate0_.id=?
Hibernate:
select
children0_.parent_id as parent2_1_,
children0_.id as id1_,
children0_.id as id4_0_,
children0_.parent_id as parent2_4_0_,
children0_.cate_no as cate3_4_0_,
children0_.cate_name as cate4_4_0_
from
goodscate children0_
where
children0_.parent_id=?
Hibernate:
select
children0_.parent_id as parent2_1_,
children0_.id as id1_,
children0_.id as id4_0_,
children0_.parent_id as parent2_4_0_,
children0_.cate_no as cate3_4_0_,
children0_.cate_name as cate4_4_0_
from
goodscate children0_
where
children0_.parent_id=?
商品名称