Hibernate4之检索策略(十三)

Hibernate4检索策略

一、检索策略属性lazy(延迟检索)
lazy:true(默认)延迟检索; set端,一对多
lazy:false 立即检索; set端 一对多
lazy:extra 增强延迟检索; set端 一对多
lazy:proxy(默认) 延迟检索 many-to-one 多对一
lazy:no-proxy 无代理延迟检索 many-to-one 多对一(需要编译时字节码增强)
二、检索策略属性batch-size(批量检索)
延迟批量检索
立即批量检索
三、检索策略属性fetch(查询检索)
fetch:select(默认) 查询方式
fetch: subselect 子查询方式
fetch: join 迫切左外连接查询方式

四:上代码

1.配置hibernate.cfg.xml文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!--数据库连接设置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>


        <!-- 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- 控制台显示SQL -->
        <property name="show_sql">true</property>

        <!-- 自动更新表结构 -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/newbeedaly/model/Student.hbm.xml"/>

        <mapping resource="com/newbeedaly/model/Class.hbm.xml"/>


    </session-factory>

</hibernate-configuration>

2.编写HibernateUtil类

package com.newbeedaly.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

    private static final SessionFactory sessionFactory=buildSessionFactory();

    private static SessionFactory buildSessionFactory(){
        Configuration configuration=new Configuration().configure(); // 实例化配置文件
        ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
        return configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}

3.编写Student、Class类

package com.newbeedaly.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 + "]";
    }


}
package com.newbeedaly.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;
    }


}

4.编写Class.hbm.xml和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.newbeedaly.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" fetch="join" >
            <key column="classId"></key>
            <one-to-many class="com.newbeedaly.model.Student"/>
        </set>
    </class>

</hibernate-mapping>
<?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.newbeedaly.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 中添加 outer-join="true" 为左外连接  -->
        <many-to-one name="c" column="classId" class="com.newbeedaly.model.Class" cascade="save-update" lazy="no-proxy"></many-to-one>
    </class>

</hibernate-mapping>

5.编写StudentTest测试类

package com.newbeedaly.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.newbeedaly.model.Class;
import com.newbeedaly.model.Student;
import com.newbeedaly.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());
        // studentList.iterator();
    }


    @Test
    public void testLazy2() {
        Student student=(Student)session.get(Student.class, Long.valueOf(1));
        student.getC().getName();
    }

    @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();
    }

    @Test
    public void testBatch2(){
        List<Class> classList=session.createQuery("from Class").list();

    }


    @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();
    }

    @Test
    public void testFetch2(){
        Class c=(Class)session.get(Class.class, Long.valueOf(1));
    }
}

猜你喜欢

转载自blog.csdn.net/willdic/article/details/80518917