javaEE Hibernate, 懒加载(关联级别)。关联的对象是单个对象。

一对多关系(关联):一个Customer(客户)对应多个LinkMan(联系人)

Test.java(通过LinkMan(多)获取关联的Customer(一)):

package cn.xxx.demo;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import cn.xxx.domain.Customer;
import cn.xxx.domain.LinkMan;
import cn.xxx.utils.HibernateUtils;

//关联级别的懒加载(抓取策略:如何获取关联对象的策略) (一对多、多对多 对象产生关联) 
public class Test {
	
	@Test
	//关联级别的加载策略。 (一对多中的多) 
	//fetch:select(默认值)  单表查询
	//lazy:proxy(默认值)  由Customer的类级别的加载策略决定。
	//customer类级别加载策略 lazy:true 懒加载
	public void fun1(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		//----------------------------------------------------
		
		LinkMan lm = session.get(LinkMan.class, 3l); // get立即加载LinkMan对象。 没有立即加载关联的Customer对象。
		
		Customer customer = lm.getCustomer(); // 没有加载关联的Customer对象。
		
		System.out.println(customer);  // 使用Customer时,才会去加载Customer对象。
		
		//----------------------------------------------------
		tx.commit();
		session.close();		
	}
	
	@Test
	//fetch:select	单表查询
	//lazy:false
	//-----------------与下面的配置的执行效果相同--------------------------
	//fetch:select	单表查询
	//lazy:proxy
	//customer类级别加载策略 lazy:false 立即加载
	public void fun2(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		//----------------------------------------------------
		
		LinkMan lm = session.get(LinkMan.class, 3l); // get立即加载LinkMan对象(单表查询)。  同时立即加载关联的Customer对象(单表查询)。 (共两次查询) 
		
		Customer customer = lm.getCustomer();
		
		System.out.println(customer);
		
		//----------------------------------------------------
		tx.commit();
		session.close();
	}
	
	@Test
	//fetch:join 多表查询
	//lazy:false|proxy 失效  
	public void fun3(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		//----------------------------------------------------
		
		LinkMan lm = session.get(LinkMan.class, 3l); // get立即加载LinkMan对象。fetch:join 多表查询;同时立即加载关联的Customer对象。 
		
		Customer customer = lm.getCustomer();
		
		System.out.println(customer);
		
		//----------------------------------------------------
		tx.commit();
		session.close();
	}
}

LinkMan.hbm.xml(配置文件;懒加载、抓取策略配置,一对多中的多)(一般采用默认配置):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.xxx.domain" >
	<class name="LinkMan" table="cst_linkman" >
		<id name="lkm_id"  >
			<generator class="native"></generator>
		</id>
		<property name="lkm_gender"  ></property>
		<property name="lkm_name"  ></property>
		<property name="lkm_phone"  ></property>
		<property name="lkm_email"  ></property>
		<property name="lkm_qq"  ></property>
		<property name="lkm_mobile"  ></property>
		<property name="lkm_memo"  ></property>
		<property name="lkm_position"  ></property>
		<!-- 
			fetch 加载关联对象时,使用的sql语句
				select(默认值): 使用单表查询
				join: 多表查询
			lazy 决定加载时机
				false: 立即加载
				proxy(默认值): 由customer的类级别加载策略决定.
		 -->
		<many-to-one name="customer" column="lkm_cust_id" class="Customer" fetch="select" lazy="proxy"  >
		</many-to-one>
	</class>
</hibernate-mapping>

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/81095573