Hibernate关系映射1:单向N-1关联
单向 n-1 关联只需从 n 的一端可以访问 1 的一端。
Hibernate 使用 元素来映射多对一关联关系<many-to-one name="customer" class="Customer" column="CUSTOMER_ID" cascade="all" />
public class Customer {
private Integer customerId;
private String customerName;
//省去get和set方法
}
public class Order {
private Integer orderId;
private String orderName;
private Customer customer;
//省去get和set方法
}
Customer.hbm.xml
<hibernate-mapping>
<class name="com.lihui.hibernate.single_n_1.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>
Order.hbm.xml
<hibernate-mapping package="com.lihui.hibernate.single_n_1">
<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="customer" class="Customer" column="CUSTOMER_ID" cascade="all" />
</class>
</hibernate-mapping>
Junit Test由于在Order.hbm.xml中配置了cascade="all"
,所以只需要保存order对象即可,会首先自动保存级联的Customer对象。
@Test
public void testSave() {
Customer customer = new Customer();
customer.setCustomerName("a");
Order order1 = new Order();
order1.setOrderName("A");
order1.setCustomer(customer);
Order order2 = new Order();
order2.setOrderName("B");
order2.setCustomer(customer);
session.save(order1);
session.save(order2);
}
Hibernate关系映射2:双向1-N关联
对于1-N关联,Hibernate推荐使用双向关联,而且不要让1的一端控制关联关系,而使用N的一端控制关联关系。
双向的N-1关联与1-N关联是完全相同的两种情形。两端都需要增加对关联属性的访问,N的一端增加引用到关联实体的属性,1的一端增加集合属性,集合元素为关联实体。
public class Customer {
private Integer customerId;
private String customerName;
private Set<Order> orders = new HashSet<Order>();
//省去get和set
}
public class Order {
private Integer orderId;
private String orderName;
private Customer customer;
//省去get和set
}
Customer.hbm.xml
<hibernate-mapping package="com.lihui.hibernate.double_n_1">
<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>
<set name="orders" inverse="true">
<key column="CUSTOMER_ID"></key>
<one-to-many class="Order"/>
</set>
</class>
</hibernate-mapping>
Hibernate关系映射3:双向1-1关联
双向1-1关联需要修改两边的持久化类代码,让两个持久化类都增加引用关联实体的属性,并为该属性提供get和set方法。
双向1-1关联有三种映射模式:
- 基于主键
- 基于外键
- 使用连接表
public class Department {
private Integer deptId;
private String deptName;
private Manager manager;
//省去get和set
}
public class Manager {
private Integer mgrId;
private String mgrName;
private Department department;
//省去get和set
}
Manager.hbm.xml
<hibernate-mapping package="com.lihui.hibernate.double_1_1.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的关联关系,在对应的数据表中已经有外键,当前持久花类使用ont-to-one进行映射 -->
<one-to-one name="department" class="Department" property-ref="manager"></one-to-one>
</class>
</hibernate-mapping>
Department.hbm.xml
<hibernate-mapping package="com.lihui.hibernate.double_1_1.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 name="manager" class="Manager" column="MGR_ID" unique="true"></many-to-one>
</class>
</hibernate-mapping>
测试
@Test
public void testGet(){
Department dep = (Department) session.get(Department.class, 1);
System.out.println(dep.getDeptName());
Manager mgr =dep.getManager();
System.out.println(mgr.getMgrName());
}
@Test
public void testSave() {
Department dep = new Department();
dep.setDeptName("a");
Manager mgr = new Manager();
mgr.setMgrName("b");
dep.setManager(mgr);
mgr.setDepartment(dep);
session.save(mgr);
session.save(dep);
}
基于主键
Manager.hbm.xml
<hibernate-mapping package="com.lihui.hibernate.double_1_1.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="department" class="Department"></one-to-one>
</class>
</hibernate-mapping>
Department.hbm.xml
<hibernate-mapping package="com.lihui.hibernate.double_1_1.primary">
<class name="Department" table="DEPARTMENTS">
<id name="deptId" type="java.lang.Integer">
<column name="DEPT_ID" />
<generator class="foreign" >
<param name="property">manager</param>
</generator>
</id>
<property name="deptName" type="java.lang.String">
<column name="DEPT_NAME" />
</property>
<one-to-one name="manager" class="Manager" constrained="true"></one-to-one>
</class>
</hibernate-mapping>