1.单向多对一
1)数据库表结构
Order:ORDER_ID,ORDER_NAME,CUSTOMER_ID
Customer:CUSTOMER_ID,CUSTOMER_NAME
2)entity:多的一端含有一的一端这个字段属性
public class Order {
private Integer orderId;
private String orderName;
//一的一端
private Customer customer;
}
public class Customer {
private Integer customerId;
private String customerName;
}
3)映射文件:一的一端正常配置,多的一端配置many-to-one标签
order.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ithings.entities">
<class name="Order" table="ORDERS">
<id name="orderId" type="java.lang.Integer">
<column name="ORDER_ID" />
<generator class="native" />
</id>
<property name="orderName" type="java.lang.String">
<column name="ORDER_NAME" />
</property>
<!--
映射多对一的关联关系。 使用 many-to-one 来映射多对一的关联关系
name: 多这一端关联的一那一端的属性的名字
class: 一那一端的属性对应的类名
column: 一那一端在多的一端对应的数据表中的外键的名字
-->
<many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one>
</class>
</hibernate-mapping>
customer.hbm.xml
<?xml version="1.0"?>
<!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.ithings.entities.Customer" table="CUSTOMERS">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="native" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
</class>
</hibernate-mapping>
2.双向多对一
1)数据库表结构
Order:ORDER_ID,ORDER_NAME,CUSTOMER_ID
Customer:CUSTOMER_ID,CUSTOMER_NAME
2)entity:多的一端含有一的一端这个字段属性,一的一端含有多的一端的集合属性
customer
public class Customer {
private Integer customerId;
private String customerName;
/*
* 1. 声明集合类型时, 需使用接口类型, 因为 hibernate 在获取
* 集合类型时, 返回的是 Hibernate 内置的集合类型, 而不是 JavaSE 一个标准的
* 集合实现.
* 2. 需要把集合进行初始化, 可以防止发生空指针异常
*/
private Set<Order> orders = new HashSet<>();
}
3)映射文件:一的一端配置set标签,多的一端配置many-to-one标签
customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.entities.n21.both">
<class name="Customer" table="CUSTOMERS">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="native" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
<!-- 映射 1 对多的那个集合属性 -->
<!-- set: 映射 set 类型的属性, table: set 中的元素对应的记录放在哪一个数据表中. 该值需要和多对一的多的那个表的名字一致 -->
<set name="orders" table="ORDERS">
<!-- 执行多的表中的外键列的名字 -->
<key column="CUSTOMER_ID"></key>
<!-- 指定映射类型 -->
<one-to-many class="Order"/>
</set>
</class>
3 一对一
3.1 一对一(基于外键)
1)数据库表结构
Manager:MGR_ID,MGR_NAME
Department:DEPT_ID,DEPT_NAME,MGR_ID
2)entity
Manager:
public class Manager {
private Integer mgrId;
private String mgrName;
private Department dept;
}
Department:
public class Department {
private Integer deptId;
private String deptName;
private Manager mgr;
}
3)映射文件
Manager.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.one2one.foreign">
<class name="Manager" table="MANAGERS">
<id name="mgrId" type="java.lang.Integer">
<column name="MGR_ID" />
<generator class="native" />
</id>
<property name="mgrName" type="java.lang.String">
<column name="MGR_NAME" />
</property>
<!-- 映射 1-1 的关联关系: 在对应的数据表中已经有外键了, 当前持久化类使用 one-to-one 进行映射 -->
<!--
没有外键的一端需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体主键以外的字段作为关联字段
-->
<one-to-one name="dept" class="Department" property-ref="mgr"></one-to-one>
</class>
</hibernate-mapping>
Department.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.one2one.foreign">
<class name="Department" table="DEPARTMENTS">
<id name="deptId" type="java.lang.Integer">
<column name="DEPT_ID" />
<generator class="native" />
</id>
<property name="deptName" type="java.lang.String">
<column name="DEPT_NAME" />
</property>
<!-- 使用 many-to-one 的方式来映射 1-1 关联关系 -->
<many-to-one name="mgr" class="Manager"
column="MGR_ID" unique="true"></many-to-one>
</class>
</hibernate-mapping>
3.2 一对一(基于主键)
1)数据库表结构
Manager:MGR_ID,MGR_NAME
Department:DEPT_ID,DEPT_NAME
2)entity
Manager:
public class Manager {
private Integer mgrId;
private String mgrName;
private Department dept;
}
Department:
public class Department {
private Integer deptId;
private String deptName;
private Manager mgr;
}
3)映射文件
Manager.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.one2one.primary">
<class name="Manager" table="MANAGERS">
<id name="mgrId" type="java.lang.Integer">
<column name="MGR_ID" />
<generator class="native" />
</id>
<property name="mgrName" type="java.lang.String">
<column name="MGR_NAME" />
</property>
<one-to-one name="dept" class="Department"></one-to-one>
</class>
</hibernate-mapping>
Department.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.one2one.primary">
<class name="Department" table="DEPARTMENTS">
<id name="deptId" type="java.lang.Integer">
<column name="DEPT_ID" />
<!-- 使用外键的方式来生成当前的主键 -->
<generator class="foreign">
<!-- property 属性指定使用当前持久化类的哪一个属性的主键作为外键 -->
<param name="property">mgr</param>
</generator>
</id>
<property name="deptName" type="java.lang.String">
<column name="DEPT_NAME" />
</property>
<!--
采用 foreign 主键生成器策略的一端增加 one-to-one 元素映射关联属性,
其 one-to-one 节点还应增加 constrained=true 属性, 以使当前的主键上添加外键约束
-->
<one-to-one name="mgr" class="Manager" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
4.多对多
1)数据库表结构
Category:ID、NAME
Item:ID、NAME
categers_items:C_ID、I_ID
2)entity
public class Category {
private Integer id;
private String name;
private Set<Item> items = new HashSet<>();
}
Item:
public class Item {
private Integer id;
private String name;
private Set<Category> categories = new HashSet<>();
}
3)映射文件
Category.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.n2n">
<class name="Category" table="CATEGORIES">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<!-- table: 指定中间表 -->
<set name="items" table="CATEGORIES_ITEMS">
<key>
<column name="C_ID" />
</key>
<!-- 使用 many-to-many 指定多对多的关联关系. column 执行 Set 集合中的持久化类在中间表的外键列的名称 -->
<many-to-many class="Item" column="I_ID"></many-to-many>
</set>
</class>
</hibernate-mapping>
Item.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.n2n">
<class name="Item" table="ITEMS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<set name="categories" table="CATEGORIES_ITEMS" inverse="true">
<key column="I_ID"></key>
<many-to-many class="com.atguigu.hibernate.n2n.Category" column="C_ID"></many-to-many>
</set>
</class>
</hibernate-mapping>
5 继承关系
5.1 继承关系 subclass
1)数据库表结构
PERSONS:id、type、name、age、school
2)entity
Person:
public class Person {
private Integer id;
private String name;
private int age;
}
Student:
public class Student extends Person{
private String school;
}
3)映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.subclass">
<class name="Person" table="PERSONS" discriminator-value="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<!-- 配置辨别者列 -->
<discriminator column="TYPE" type="string"></discriminator>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<!-- 映射子类 Student, 使用 subclass 进行映射 -->
<subclass name="Student" discriminator-value="STUDENT">
<property name="school" type="string" column="SCHOOL"></property>
</subclass>
</class>
</hibernate-mapping>
5.2 继承关系 joined-subclass
1)数据库表结构
PERSONS:id、name、age
STUDENTS:school_id、school
2)entity
Person:
public class Person {
private Integer id;
private String name;
private int age;
}
Student:
public class Student extends Person{
private String school;
}
3)映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.joined.subclass">
<class name="Person" table="PERSONS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<!--采用 joined-subclass 元素的继承映射可以实现每个子类一张表-->
<joined-subclass name="Student" table="STUDENTS">
<key column="STUDENT_ID"></key>
<property name="school" type="string" column="SCHOOL"></property>
</joined-subclass>
</class>
</hibernate-mapping>
5.3 继承关系 union-subclass
1)数据库表结构
PERSONS:id、name、age
STUDENTS:id、name、age、school
2)entity
Person:
public class Person {
private Integer id;
private String name;
private int age;
}
Student:
public class Student extends Person{
private String school;
}
3)映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.hibernate.union.subclass">
<class name="Person" table="PERSONS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="hilo" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<union-subclass name="Student" table="STUDENTS">
<property name="school" column="SCHOOL" type="string"></property>
</union-subclass>
</class>
</hibernate-mapping>