栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Hibernate 立即检索策略和延迟检索策略

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hibernate 立即检索策略和延迟检索策略

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




    
        
            
        
        
        



        
            
            
        
    

这里使用了一次查询语句

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号