hibernate 1-1(具体解释)
域模型
关系数据模型:
- 依照外键映射:
- 依照主键映射:
- 对于基于外键的1-1关联。其外键能够存放在随意一边。在须要存放外键一端。添加many-to-one元素。为many-to-one元素添加unique=“true” 属性来表示为1-1关联
- 还有一端须要使用one-to-one元素。该元素使用 property-ref 属性指定使用被关联实体主键以外的字段作为关联字段
-不使用 property-ref 属性的 sql-使用 property-ref 属性的 sql
为什么不两边都使用外键映射的 1-1
这样就不是一对一关系映射了。
基于主键映射的 1-1
- 基于主键的映射策略:指一端的主键生成器使用 foreign 策略,表明依据”对方”的主键来生成自己的主键,自己并不能独立生成主键. <param> 子元素指定使用当前持久化类的哪个属性作为 “对方”
- 採用foreign主键生成器策略的一端添加 one-to-one 元素映射关联属性。其one-to-one属性还应添加 constrained=“true” 属性;还有一端添加one-to-one元素映射关联属性。
- constrained(约束):指定为当前持久化类相应的数据库表的主键加入一个外键约束,引用被关联的对象(“对方”)所相应的数据库表主键
基于外键映射的 1-1
实例具体解释:
Department.java
package com.atguigu.hibernate.one2one.foreign;
public class Department {
private Integer deptId;
private String deptName;
private Manager mgr;
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Manager getMgr() {
return mgr;
}
public void setMgr(Manager mgr) {
this.mgr = mgr;
}
}
package com.atguigu.hibernate.one2one.foreign;
public class Manager {
private Integer mgrId;
private String mgrName;
private Department dept;
public Integer getMgrId() {
return mgrId;
}
public void setMgrId(Integer mgrId) {
this.mgrId = mgrId;
}
public String getMgrName() {
return mgrName;
}
public void setMgrName(String mgrName) {
this.mgrName = mgrName;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
}
<?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.atguigu.hibernate.one2one.foreign.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="com.atguigu.hibernate.one2one.foreign.Manager"
column="MGR_ID" unique="true"></many-to-one>
</class>
</hibernate-mapping>
<?
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.atguigu.hibernate.one2one.foreign.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="com.atguigu.hibernate.one2one.foreign.Department" property-ref="mgr"></one-to-one> </class> </hibernate-mapping>
package com.atguigu.hibernate.one2one.foreign;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HibernateTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testGet2(){
//在查询没有外键的实体对象时, 使用的左外连接查询, 一并查询出其关联的对象
//并已经进行初始化.
Manager mgr = (Manager) session.get(Manager.class, 1);
System.out.println(mgr.getMgrName());
System.out.println(mgr.getDept().getDeptName());
}
@Test
public void testGet(){
//1. 默认情况下对关联属性使用懒载入
Department dept = (Department) session.get(Department.class, 1);
System.out.println(dept.getDeptName());
//2. 所以会出现懒载入异常的问题.
// session.close();
// Manager mgr = dept.getMgr();
// System.out.println(mgr.getClass());
// System.out.println(mgr.getMgrName());
//3. 查询 Manager 对象的连接条件应该是 dept.manager_id = mgr.manager_id
//而不应该是 dept.dept_id = mgr.manager_id
Manager mgr = dept.getMgr();
System.out.println(mgr.getMgrName());
}
@Test
public void testSave(){
Department department = new Department();
department.setDeptName("DEPT-BB");
Manager manager = new Manager();
manager.setMgrName("MGR-BB");
//设定关联关系
department.setMgr(manager);
manager.setDept(department);
//保存操作
//建议先保存没有外键列的那个对象. 这样会降低 UPDATE 语句
session.save(department);
session.save(manager);
}
}
基于主键映射的 1-1
实例具体解释:
实体类不变和上面同样
Department.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="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>
<?
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.atguigu.hibernate.one2one.primary.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="com.atguigu.hibernate.one2one.primary.Department"></one-to-one> </class> </hibernate-mapping>
package com.atguigu.hibernate.one2one.primary;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HibernateTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testGet2(){
//在查询没有外键的实体对象时, 使用的左外连接查询, 一并查询出其关联的对象
//并已经进行初始化.
Manager mgr = (Manager) session.get(Manager.class, 1);
System.out.println(mgr.getMgrName());
System.out.println(mgr.getDept().getDeptName());
}
@Test
public void testGet(){
//1. 默认情况下对关联属性使用懒载入
Department dept = (Department) session.get(Department.class, 1);
System.out.println(dept.getDeptName());
//2. 所以会出现懒载入异常的问题.
Manager mgr = dept.getMgr();
System.out.println(mgr.getMgrName());
}
@Test
public void testSave(){
Department department = new Department();
department.setDeptName("DEPT-DD");
Manager manager = new Manager();
manager.setMgrName("MGR-DD");
//设定关联关系
manager.setDept(department);
department.setMgr(manager);
//保存操作
//先插入哪一个都不会有多余的 UPDATE
session.save(department);
session.save(manager);
}
}
posted on 2019-03-29 12:05 xfgnongmin 阅读(152) 评论(0) 编辑 收藏 举报