標籤:hibernate
1、get、load方法區別?
get: 及時載入,只要調用get方法立刻向資料庫查詢
load:預設使用懶載入,當用到資料的時候才向資料庫查詢。
2、懶載入(lazy)
2.1、概念
概念:當用到資料的時候才向資料庫查詢,這就是hibernate的懶載入特性。
目的:提升程式執行效率!
2.2、lazy屬性的值
在Hibernate中,懶載入對應lazy屬性的值。
lazy屬性的值
| 序號 |
值 |
含義 |
| 1 |
true |
使用懶載入 |
| 2 |
false |
關閉懶載入 |
| 3 |
extra |
在集合屬性(配置當中使用set標籤)懶載入的時候提升效率
在真正使用資料的時候才向資料庫發送查詢的sql;
如果調用集合的size()/isEmpty()方法,只是統計,不真正查詢資料! |
2.3、懶載入異常
(1)Session關閉後,不能使用懶載入資料!
(2)如果session關閉後,使用懶載入資料報錯:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
(3)如何解決session關閉後不能使用懶載入資料的問題?
方式1: 先使用一下資料
dept.getDeptName();
方式2:強迫代理對象初始化
Hibernate.initialize(dept);
方式3:關閉懶載入
在.hbm.xml設定檔中設定lazy=false;
方式4: 在使用資料之後,再關閉session!
方式1和方式4的區別是:方式1是說,如果想在關閉Session後,仍然想用懶載入的資料,可以先使用一下需要懶載入的資料,這樣就會提前載入那些資料;方式4是說,既然懶載入的資料在Session關閉後不能再進行訪問,那麼它就建議在Session關閉之前使用。
3、範例程式碼
Department.java
package com.rk.hibernate.j_lazy;import java.util.Set;public class Department{private int deptId;private String deptName;private Set<Employee> emps;public int getDeptId(){return deptId;}public void setDeptId(int deptId){this.deptId = deptId;}public String getDeptName(){return deptName;}public void setDeptName(String deptName){this.deptName = deptName;}public Set<Employee> getEmps(){return emps;}public void setEmps(Set<Employee> emps){this.emps = emps;}@Overridepublic String toString(){return "Deparment [deptId=" + deptId + ", deptName=" + deptName + "]";}}
Department.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="com.rk.hibernate.j_lazy" auto-import="true"><!-- class標籤內可以設定lazy屬性 --><class name="Department" table="T_Department" lazy="false"><id name="deptId" column="id"><generator class="native"></generator></id><property name="deptName" column="name"></property><!-- 集合屬性,預設使用懶載入 lazytrue 懶載入extra 懶載入(智能)false 關閉懶載入--><set name="emps" table="T_Employee" lazy="extra"><key column="deptId"></key><one-to-many class="Employee"/></set></class></hibernate-mapping>
Employee.java
package com.rk.hibernate.j_lazy;public class Employee{private int empId;private String empName;private int salary;private Department dept;public int getEmpId(){return empId;}public void setEmpId(int empId){this.empId = empId;}public String getEmpName(){return empName;}public void setEmpName(String empName){this.empName = empName;}public int getSalary(){return salary;}public void setSalary(int salary){this.salary = salary;}public Department getDept(){return dept;}public void setDept(Department dept){this.dept = dept;}@Overridepublic String toString(){return "Employee [empId=" + empId + ", empName=" + empName + ", salary=" + salary + "]";}}
Employee.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="com.rk.hibernate.j_lazy" auto-import="true"><class name="Employee" table="T_Employee"><id name="empId" column="id"><generator class="native"></generator></id><property name="empName" column="name"></property><property name="salary" column="salary"></property><many-to-one name="dept" column="deptId" class="Department"></many-to-one></class></hibernate-mapping>
App.java
package com.rk.hibernate.j_lazy;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App{private static SessionFactory sf;static{sf = new Configuration().configure().addClass(Department.class).addClass(Employee.class).buildSessionFactory();}//1. 主鍵查詢,get和load的區別//這裡是對JavaBean類的懶載入@Testpublic void testClassGetLoad(){Session session = sf.openSession();session.beginTransaction();// get: 及時查詢//Department dept = (Department)session.get(Department.class, 1);//System.out.println(dept.getDeptName());//System.out.println(dept.getEmps());// load,預設懶載入, 及在使用資料的時候,才向資料庫發送查詢的sql語句!Department dept = (Department)session.load(Department.class, 1);// 方式1: 先使用一下資料// dept.getDeptName();// 方式2:強迫代理對象初始化// Hibernate.initialize(dept);// 方式3:關閉懶載入// 設定檔中將lazy設定為false。<class name="Department" table="T_Department" lazy="false">session.getTransaction().commit();session.close();// session關閉後,是否能夠使用System.out.println(dept.getDeptName());}//2.這裡是對屬性(外部索引鍵關聯)的懶載入@Testpublic void testProperty(){Session session = sf.openSession();session.beginTransaction();Department dept = (Department)session.get(Department.class, 1);System.out.println(dept.getDeptName());System.out.println("--------------------------");System.out.println(dept.getEmps().size());System.out.println(dept.getEmps().isEmpty());System.out.println(dept.getEmps());session.getTransaction().commit();session.close();}}
(11)Hibernate懶載入