(九)Hibernate 检索策略

时间:2022-09-11 05:50:24

所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java

这里的hibernate.cfg.xml配置信息我就不再写了

第一节:检索策略属性Lazy

Lazy:true (默认) 延迟检索;set 端一对多

Lazy:false 立即检索;set 端一对多

Lazy:extra 增强延迟检索; set 端一对多

Lazy:proxy(默认) 延迟检索;many-to-one 多对一

Lazy:no-proxy 无代理延迟检索;many-to-one 多对一(需要编译时字节码增强)

Lazy:true (默认) 延迟检索;set 端一对多

Class.java

 package com.wishwzp.model;

 import java.util.HashSet;
import java.util.Set; public class Class { private long id;
private String name;
private Set<Student> students=new HashSet<Student>(); public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
} }

Student.java

 package com.wishwzp.model;

 public class Student {

     private long id;
private String name;
private Class c; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public Class getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="true">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

Student.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id> <property name="name" column="stuName"></property> <many-to-one name="c" column="classId" class="com.wishwzp.model.Class" cascade="save-update"></many-to-one>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testLazy1() {
Class c=(Class)session.get(Class.class, Long.valueOf(1));
Set<Student> studentList=(Set<Student>)c.getStudents();
studentList.iterator();
}
}

数据库里面的信息数据:

(九)Hibernate 检索策略

(九)Hibernate 检索策略

(九)Hibernate 检索策略

运行显示结果:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?
Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=?

Lazy:false 立即检索;set 端一对多

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="false">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testLazy1() {
Class c=(Class)session.get(Class.class, Long.valueOf(1)); } }

还是上面第一个的数据库信息。

运行显示结果:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?
Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=?

Lazy:extra 增强延迟检索; set 端一对多

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="extra">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testLazy1() {
Class c=(Class)session.get(Class.class, Long.valueOf(1));
Set<Student> studentList=(Set<Student>)c.getStudents();
System.out.println(studentList.size());
} }

还是上面第一个的数据库信息。

运行结果显示:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?
Hibernate: select count(stuId) from t_student where classId =?
3

Lazy:proxy(默认) 延迟检索;many-to-one 多对一

Student.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id> <property name="name" column="stuName"></property> <many-to-one name="c" column="classId" class="com.wishwzp.model.Class" cascade="save-update" lazy="proxy"></many-to-one>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testLazy2() {
Student student=(Student)session.get(Student.class, Long.valueOf(1));
student.getC().getName();
} }

运行显示结果:

Hibernate: select student0_.stuId as stuId1_1_0_, student0_.stuName as stuName2_1_0_, student0_.classId as classId3_1_0_ from t_student student0_ where student0_.stuId=?
Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?

Lazy:no-proxy 无代理延迟检索;many-to-one 多对一(需要编译时字节码增强)

Student.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id> <property name="name" column="stuName"></property> <many-to-one name="c" column="classId" class="com.wishwzp.model.Class" cascade="save-update" lazy="no-proxy"></many-to-one>
</class> </hibernate-mapping>

StudentTest.java和上面一样

第二节:检索策略属性batch-size

1,批量延迟检索;

2,批量立即检索;

1,批量延迟检索;

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="true" batch-size="3">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testBatch1(){
List<Class> classList=session.createQuery("from Class").list();
Iterator it=classList.iterator();
Class c1=(Class)it.next();
Class c2=(Class)it.next();
Class c3=(Class)it.next();
c1.getStudents().iterator();
c2.getStudents().iterator();
c3.getStudents().iterator();
} }

数据库多加了一条信息:

(九)Hibernate 检索策略

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?, ?)

2,批量立即检索;

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="3">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testBatch2(){
List<Class> classList=session.createQuery("from Class").list(); } }

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?, ?)

假如我们将Class.hbm.xml的batch-size="3"改成batch-size="2"的话,运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?)
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId=?

第三节:检索策略属性Fetch

1,Fetch:select(默认) 查询方式;

2,Fetch:subselect 子查询方式;

3,Fetch:join 迫切左外连接查询方式;

1,Fetch:select(默认) 查询方式;

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="2" fetch="select">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testFetch1(){
List<Class> classList=session.createQuery("from Class").list();
Iterator it=classList.iterator();
Class c1=(Class)it.next();
Class c2=(Class)it.next();
Class c3=(Class)it.next();
c1.getStudents().iterator();
c2.getStudents().iterator();
c3.getStudents().iterator();
} }

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?)
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId=?

2,Fetch:subselect 子查询方式;

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="2" fetch="subselect">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testFetch1(){
List<Class> classList=session.createQuery("from Class").list();
Iterator it=classList.iterator();
Class c1=(Class)it.next();
Class c2=(Class)it.next();
Class c3=(Class)it.next();
c1.getStudents().iterator();
c2.getStudents().iterator();
c3.getStudents().iterator();
} }

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (select class0_.classId from t_class class0_)

3,Fetch:join 迫切左外连接查询方式;

Class.hbm.xml

 <?xml version="1.0"?>
<!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.wishwzp.model"> <class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id> <property name="name" column="className"></property> <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="2" fetch="join">
<key column="classId"></key>
<one-to-many class="com.wishwzp.model.Student"/>
</set>
</class> </hibernate-mapping>

StudentTest.java

 package com.wishwzp.service;

 import java.util.Iterator;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Class;
import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testFetch2(){
Class c=(Class)session.get(Class.class, Long.valueOf(1));
}
}

运行结果显示:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_, students1_.classId as classId3_0_1_, students1_.stuId as stuId1_1_1_, students1_.stuId as stuId1_1_2_, students1_.stuName as stuName2_1_2_, students1_.classId as classId3_1_2_ from t_class class0_ left outer join t_student students1_ on class0_.classId=students1_.classId where class0_.classId=?


END