延迟加载:延迟加载(lazy load懒加载)是当在真正需要数据时,才执行SQL语句进行查询,避免了无谓的性能开销。
延迟加载分类:
01.类级别的查询策略
02.一对多和多对多关联的查询策略
03.多对一关联的查询策略
1.类级别可选的检索策略包括立即检索和延迟检索,默认为延迟检索
1.1立即检索:立即加载检索方法指定的对象,立即发送SQL
1.2 延迟检索:延迟加载检索方法制定的对象.在使用具体的属性时,再进行加载,才发送SQL
2.无论
3.若
3.1 由Hibernate在运行时采用CGLIB工具动态生成
3.2 Hibernate创建代理类实例时,仅初始化其OID属性
3.3在应用程序第一次访问代理类实例的非OID属性时,Hibernate会初始化代理类实例
3.4注意:类级别检索策略仅适用于load()方法
(立即加载,延迟加载等)检索,通过set元素的lazy属性设置。
01.一对多或者多对多检索策略由lazy和fetch共同确定
Lazy:决定关联对象初始化时机
Fetch:决定SQL语句构建形式
02.fetch取值
Join:迫切左外连接
Select:多条简单SQL(默认值)
Subselect:子查询
03.fetch和lazy组合
解析:fetch=”join” lazy会被忽略,迫切左外连接的立即检索
Fetch=”select” lazy=”false” 多条简单SQL立即检索
Fetch=”select” lazy=”true” 多条语句延迟检索
Fetch=”select” lazy=”extra” 多条语句及其懒惰检索
Fetch=”subselect” lazy=”false” 子查询立即检索
Fetch=”subselect” lazy=”true” 子查询延迟检索
Fetch=”subselect” lazy=”extra” 子查询及其懒惰检索
Extra:极其懒惰,只有访问集合对象的属性时才会加载,访问集合本身的属性时(例如,集合大小,生成count),不会立即加载。
注意:query的list()会忽略映射文件配置的左外连接查询,此时lazy属性重新生效。
具体案例
首先我们用的hibernate.hbm.xml都是一个的 直接上传一个就ok. 连接数据库 对应哪个包下仔细看一下。
oracle.jdbc.OracleDriver
jdbc:oracle:thin:@localhost:1521:orcl
sll
sll
org.hibernate.dialect.Oracle10gDialect
thread
true
update
true
然后因为这个没有用到注解 所以我们需要小配置
首先我们看一下实体类。
package cn.happy.hibernate11lazyLoad;
import java.util.Date;
/**
* Created by linlin on 2017/10/9.
*/
public class Student {
private Integer stuno;
private String stuname;
private Integer stuage;
private Date studate;
public Integer getStuno() {
return stuno;
}
public void setStuno(Integer stuno) {
this.stuno = stuno;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public Integer getStuage() {
return stuage;
}
public void setStuage(Integer stuage) {
this.stuage = stuage;
}
public Date getStudate() {
return studate;
}
public void setStudate(Date studate) {
this.studate = studate;
}
}
每个 案例也就那几步
小配置 :
测试类:
package cn.happy.test;
import cn.happy.hibernate11lazyLoad.Dept;
import cn.happy.hibernate11lazyLoad.Emp;
import cn.happy.hibernate11lazyLoad.Student;
import cn.happy.hibernate12nosession.biz.HibernateBiz;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.Set;
/**
* Created by linlin on 2017/9/22.
*/
public class test1009 {
Configuration cfg;
Transaction tx;
Session session;
SessionFactory factory;
@Before
public void myBefore() {
//创建配置对象
cfg = new Configuration().configure("Hibernate.cfg.xml");
//根据配置对象创建SessionFactory
factory = cfg.buildSessionFactory();
//根据SessionFactory创建Session
session = factory.openSession();
//在Session创建后开启事务
tx = session.beginTransaction();
}
@Test
public void testmanyToOne() {
Dept dept = session.get(Dept.class, 14);
Set emps = dept.getEmps();
emps.size();
}
@Test
public void testmanyToOne0() {
Student student = session.load(Student.class, 1);
}
}
这里下面的方法是上面测试
下面这个是实体类和小配置是上面的方法
package cn.happy.hibernate11lazyLoad;
import java.util.HashSet;
import java.util.Set;
/**
* Created by linlin on 2017/9/24.
*/
public class Dept {
private Integer deptno;
private String deptname;
private Set emps=new HashSet();
public Set getEmps() {
return emps;
}
public void setEmps(Set emps) {
this.emps = emps;
}
public Dept() {
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
}
package cn.happy.hibernate11lazyLoad;
/**
* Created by linlin on 2017/9/24.
*/
public class Emp {
private Integer empno;
private String ename;
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
小配置:
上面是延迟加载的 案例
下面是no-session
biz:
package cn.happy.hibernate12nosession.biz;
import cn.happy.hibernate12nosession.Emp;
import cn.happy.hibernate12nosession.dao.HibernateDAO;
import cn.happy.hibernate12nosession.util.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Transaction;
import java.io.Serializable;
/**
* Created by linlin on 2017/10/9.
*/
public class HibernateBiz {
HibernateDAO dao=new HibernateDAO();
public Object get(Class clazz, Serializable id) {
Transaction tx = HibernateUtil.currentSession().beginTransaction();
Object obj= dao.get(clazz, id);
/* Emp emp=(Emp)obj;
emp.getEname();*/
if(!Hibernate.isInitialized(obj)){
Hibernate.initialize(obj);
}
System.out.println("==============================================");
tx.commit();
HibernateUtil.closeSession();
return obj;
}
}
package cn.happy.hibernate12nosession.dao;
import cn.happy.hibernate10opensessioninview.util.HibernateUtil;
import java.io.Serializable;
/**
* Created by linlin on 2017/10/9.
*/
public class HibernateDAO {
public Object get(Class clazz, Serializable id) {
Object result= HibernateUtil.getSession().load(clazz,id);
return result;
}
}
util:
package cn.happy.hibernate12nosession.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Created by linlin on 2017/10/6.
*/
public class HibernateUtil {
//getCurrentSession()底层实现原理
//ThreadLocal变量
public static final ThreadLocal threadTL=new ThreadLocal();
//我想直接调用一个方法,获取Session
//定义一个sessionFactory对象
private static SessionFactory factory;
private static Configuration cfg;
static{
cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
//提供一个静态方法
public static Session currentSession(){
Session session=threadTL.get();
if(session==null){ //当前线程中没有session对象
session=factory.openSession();
threadTL.set(session);
}
return session;
}
//关闭session
public static void closeSession(){
//获取线程中的session
Session session = threadTL.get();
if (session!=null) {
threadTL.set(null);
session.close();
}
}
}
测试类
package cn.happy.test;
import cn.happy.hibernate12nosession.Emp;
import cn.happy.hibernate12nosession.biz.HibernateBiz;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;
import java.util.Set;
/**
* Created by linlin on 2017/9/22.
*/
public class test1009_01 {
Configuration cfg;
Transaction tx;
Session session;
SessionFactory factory;
@Before
public void myBefore() {
//创建配置对象
cfg = new Configuration().configure("Hibernate.cfg.xml");
//根据配置对象创建SessionFactory
factory = cfg.buildSessionFactory();
//根据SessionFactory创建Session
session = factory.openSession();
//在Session创建后开启事务
tx = session.beginTransaction();
}
//no-session
@Test
public void loadGetTest() {
HibernateBiz biz = new HibernateBiz();
Emp emp = (Emp) biz.get(Emp.class, 1);
System.out.println(emp.getEname());
}
}