Hibernate 延迟检索策略
lazy=“”
true延迟检索策略
false立即检索策略
HibernateUtil
package com.csx.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
Configuration configuration=new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
catch(Throwable ex){
System.out.println("初始化sessionFactory失败"+ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session
= new ThreadLocal();
public static Session currentSession() throws HibernateException{
Session s = session.get();
if (s == null){
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException{
Session s = session.get();
if (s != null){
s.close();
}
session.set(null);
}
}
Course
package com.csx.pojo;
public class Course {
private Integer courseNo;
private String courseName;
private Student student;
public Course() {
}
public Integer getCourseNo() {
return courseNo;
}
public void setCourseNo(Integer courseNo) {
this.courseNo = courseNo;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Course{" +
"courseNo=" + courseNo +
", courseName='" + courseName + ''' +
", student=" + student +
'}';
}
}
Course.hbm.xml
Student
package com.csx.pojo;
import java.util.HashSet;
import java.util.Set;
public class Student {
private Integer id;
private String studentNo;
private String name;
//使用set来保存关联实体
private Set courses = new HashSet();
public Student() {
}
public Student(Integer id, String studentNo, String name, Set courses) {
this.id = id;
this.studentNo = studentNo;
this.name = name;
this.courses = courses;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = courses;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", studentNo='" + studentNo + ''' +
", name='" + name + ''' +
", courses=" + courses +
'}';
}
}
Student.hbm.xml
RetrievalTest
package com.csx;
import com.csx.hibernate.HibernateUtil;
import com.csx.pojo.Course;
import com.csx.pojo.Student;
import org.hibernate.Session;
import org.hibernate.Transaction;
//省略import
public class RetrievalTest {
public static void main(String[] args) {
RetrievalTest rt = new RetrievalTest();
// rt.TwoWayOneToMany();
rt.lazy();
}
private void lazy() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Student student = (Student) session.load(Student.class, 1);
System.out.println("student name:"+student.getName());
tx.commit();
HibernateUtil.closeSession();
}
public void TwoWayOneToMany(){
Session session= HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setName("woooo");
student.setStudentNo("0000");
session.save(student);
Course oracle = new Course();
oracle.setCourseName("苦逼的oracle");
oracle.setStudent(student);
session.persist(oracle);
Course j2ee = new Course();
j2ee.setCourseName("魅力四射的j2ee");
j2ee.setStudent(student);
session.persist(j2ee);
tx.commit();
HibernateUtil.closeSession();
}
}
这里使用了两次查询语句
延迟检索策略设置lazy为true
Student.hbm.xml
这里使用了一次查询语句



