Hibernate学习笔记----基于外键或主键映射的1-1关联关系

基于外键映射的1-1关联关系

Department.java

package cn.limbo.hibernate.oneToone.foreign;

public class Department {

	private Integer departmentId;
	private String departmentName;
	
	private Manager manager;

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}
	
	
	
}


Manager.java

package cn.limbo.hibernate.oneToone.foreign;

public class Manager {
	
	private Integer managerId;
	private String managerName;
	
	private Department department;

	public Integer getManagerId() {
		return managerId;
	}

	public void setManagerId(Integer managerId) {
		this.managerId = managerId;
	}

	public String getManagerName() {
		return managerName;
	}

	public void setManagerName(String managerName) {
		this.managerName = managerName;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}
	
}
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">
<!-- Generated 2016-7-21 20:48:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>

    <class name="cn.limbo.hibernate.oneToone.foreign.Department" table="DEPARTMENTS">
        <id name="departmentId" type="java.lang.Integer">
            <column name="DEPARTMENT_ID" />
            <generator class="native" />
        </id>
        
        <property name="departmentName" type="java.lang.String">
            <column name="DEPARTMENT_NAME" />
        </property>
        
        <!-- 使用many-to-one方式映射1-1 需添加unique属性-->
        <many-to-one name="manager" class="cn.limbo.hibernate.oneToone.foreign.Manager"
        column="MANAGER_ID" unique="true"></many-to-one>
        
    </class>
    
</hibernate-mapping>
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">
<!-- Generated 2016-7-21 20:48:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="cn.limbo.hibernate.oneToone.foreign">

    <class name="Manager" table="MANAGERS">
    
        <id name="managerId" type="java.lang.Integer">
            <column name="MANAGER_ID" />
            <generator class="native" />
        </id>
        
        <property name="managerName" type="java.lang.String">
            <column name="MANAGER_NAME" />
        </property>
        
        <!-- 映射1-1的关联关系 :在对应的数据表中已经有外键了,当前持久化类使用one-to-one 进行映射-->
        <!-- 
        		没有外键的一端需要使用one-to-one元素,该元素使用property-ref来属性指定使用被关联实体主键以外的字段作为关联字段
        		若不添加该属性,则manager的manager_id属性关联的是department的department_id属性,加上了才是对应department的manager属性
        -->
        <one-to-one name="department" class="Department" property-ref="manager"></one-to-one>
    </class>
    
</hibernate-mapping>



Junit.java

package cn.limbo.hibernate.oneToone.foreign;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Junit {
	
	private SessionFactory sessionFactory ;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init()
	{
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
				.applySettings(configuration.getProperties()).build();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	
	@After
	public void destroy()
	{
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	@Test
	public void testSave()
	{
		Department department = new Department();
		department.setDepartmentName("AA");
		
		Manager manager = new Manager();
		manager.setManagerName("aa");
		
		//设定关联关系
		manager.setDepartment(department);
		department.setManager(manager);
		
		//保存
		//建议先保存没有外键列的那个对象,这样会减少UPDATE语句
		session.save(manager);
		session.save(department);
		
	}
	
	@Test
	public void testGet()
	{
		//1.默认情况下对关联属性使用懒加载
		//2.所以会出现懒加载异常的情况
		Department department = (Department) session.get(Department.class, 1);
		
		//3.查询Manager对象的连接条件应该是department.manager_id = manager.manager_id
		//而不应该是department.department_id = manager.manager_id
		Manager manager = department.getManager();
		System.out.println(manager);
		
	}
	
	@Test
	public void testGet2()
	{
		//查询没有外键的实体对象的时候,使用左外连接查询,一并查出其关联的对象
		//并且已经进行初始化
		Manager manager = (Manager) session.get(Manager.class, 1);
		System.out.println(manager.getManagerName());
		System.out.println(manager.getDepartment().getDepartmentName());
	}
	
}

基于主键映射的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">
<!-- Generated 2016-7-21 20:48:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="cn.limbo.hibernate.oneToone.primary">

    <class name="Department" table="DEPARTMENTS">
    
        <id name="departmentId" type="java.lang.Integer">
            <column name="DEPARTMENT_ID" />
            <!-- 使用外键的方式生成当前的主键 -->
            <generator class="foreign">
            		<!-- 
            			property属性指定使用当前持久化类的哪一个属性的主键作为外键 
            			如:Department使用manager属性的主键作为外键
            		-->
            		<param name="property">manager</param>
            </generator>
            
        </id>
        
        <property name="departmentName" type="java.lang.String">
            <column name="DEPARTMENT_NAME" />
        </property>
        
        <!-- 
        		 采用foreign主键生成策略的一端增加one-to-one元素映射关联属性,
        		 其中one-to-one属性还应该增加constrained="true",以使当前主键添加外键约束
        		 另一端增加one-to-one元素映射关联属性 
        	-->
        <one-to-one name="manager" class="Manager" constrained="true"></one-to-one>
        
    </class>
    
</hibernate-mapping>
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">
<!-- Generated 2016-7-21 20:48:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="cn.limbo.hibernate.oneToone.primary">

    <class name="Manager" table="MANAGERS">
    
        <id name="managerId" type="java.lang.Integer">
            <column name="MANAGER_ID" />
            <generator class="native" />
        </id>
        
        <property name="managerName" type="java.lang.String">
            <column name="MANAGER_NAME" />
        </property>
        
       	<!-- 此时不需要property-ref="manager" -->
        <one-to-one name="department" class="Department"></one-to-one>
    </class>
    
</hibernate-mapping>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值