1、类级别查询:
get方法和load方法:
(1)get方法:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Student student=session.get(Student.class,201811); System.out.println(student); transaction.commit(); session.close(); }
特点:执行get方法时,立即发送查询语句查询结果。
(2)load方法(延迟加载,即:只有在使用的时候才会进行查询):
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Student student=session.load(Student.class,201811); System.out.println(student); transaction.commit(); session.close(); }
特点:在执行load方法时,不会发送任何sql语句,不会产生任何查询结果,但是会产生一个对象,在使用该对象时才会产生查询结果。
延迟加载与配置文件的关系:
延迟加载(默认):
<class name="Student" table="student" lazy="true">
不延迟加载:
<class name="Student" table="student" lazy="false">
延迟加载可以提高效率(对象取出来可以不用,到真正需要的时候才进行查询)。
2、关联级别延迟加载和抓取策略:
集合级别的关联:
lazy属性:决定是否延迟加载 true(默认值):延迟加载,懒惰加载 false:立即加载 extra:极其懒惰 fetch属性:决定加载策略,使用什么类型的sql语句加载集合数据 select(默认值):单表查询加载 join:多表查询加载集合 subselect:使用子查询加载集合
(1)lazy=true,fetch=select
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Student student=session.load(Student.class,201811);//不执行查询 Clas clas= student.getaClas(); System.out.println(clas);//执行查询 transaction.commit(); session.close(); }
<hibernate-mapping package="pers.zhb.domain"> <class name="Clas" table="class"> <id name="classno" column="classno"> <generator class="native"></generator> </id><!--主键--> <property name="department" column="department"></property> <property name="monitor" column="monitor"></property> <property name="classname" column="classname"></property> <set name="students" table="student" lazy="true" fetch="select"><!--一对多关系配置--> <key column="classno" update="false" ></key><!--指定了集合表的外键--> <one-to-many class="Student"></one-to-many> </set> </class> </hibernate-mapping>
表中的数据:
class表:
student表:
(2)lazy=false,fetch=select
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Student student=session.load(Student.class,201811);//没有使用即开始执行查询 Clas clas= student.getaClas(); System.out.println(clas); transaction.commit(); session.close(); }
(3)lazy=extra,fetch=select
极其懒惰与懒加载几乎一致。
获取查询结果的size:
public class Test{ public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Student student=session.load(Student.class,201811); Set<Clas> clas= Collections.singleton(student.getaClas()); System.out.println(clas); System.out.println(clas.size()); transaction.commit(); session.close(); }
Hibernate: select student0_.studentno as studentn1_1_0_, student0_.birthday as birthday2_1_0_, student0_.classno as classno3_1_0_, student0_.phone as phone4_1_0_, student0_.sex as sex5_1_0_, student0_.sname as sname6_1_0_, student0_.point as point7_1_0_ from student student0_ where student0_.studentno=? Hibernate: select clas0_.classno as classno1_0_0_, clas0_.department as departme2_0_0_, clas0_.monitor as monitor3_0_0_, clas0_.classname as classnam4_0_0_ from class clas0_ where clas0_.classno=? Hibernate: select students0_.classno as classno3_1_0_, students0_.studentno as studentn1_1_0_, students0_.studentno as studentn1_1_1_, students0_.birthday as birthday2_1_1_, students0_.classno as classno3_1_1_, students0_.phone as phone4_1_1_, students0_.sex as sex5_1_1_, students0_.sname as sname6_1_1_, students0_.point as point7_1_1_ from student students0_ where students0_.classno=? [Clas{classno=tx171, department=‘信工学院‘, monitor=‘张伟‘, classname=‘通信171‘, students=[Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}]}] 1
(4)fetch=select
当fetch="select" 时lazy无论是 true | false | extra都失效,都是立即加载。
Hibernate: select student0_.studentno as studentn1_1_0_, student0_.birthday as birthday2_1_0_, student0_.classno as classno3_1_0_, student0_.phone as phone4_1_0_, student0_.sex as sex5_1_0_, student0_.sname as sname6_1_0_, student0_.point as point7_1_0_ from student student0_ where student0_.studentno=? Hibernate: select clas0_.classno as classno1_0_0_, clas0_.department as departme2_0_0_, clas0_.monitor as monitor3_0_0_, clas0_.classname as classnam4_0_0_ from class clas0_ where clas0_.classno=? Hibernate: select students0_.classno as classno3_1_0_, students0_.studentno as studentn1_1_0_, students0_.studentno as studentn1_1_1_, students0_.birthday as birthday2_1_1_, students0_.classno as classno3_1_1_, students0_.phone as phone4_1_1_, students0_.sex as sex5_1_1_, students0_.sname as sname6_1_1_, students0_.point as point7_1_1_ from student students0_ where students0_.classno=? [Clas{classno=tx171, department=‘信工学院‘, monitor=‘张伟‘, classname=‘通信171‘, students=[Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}]}]
(5)lazy="true" fetch="subselect"
Hibernate: select student0_.studentno as studentn1_1_0_, student0_.birthday as birthday2_1_0_, student0_.classno as classno3_1_0_, student0_.phone as phone4_1_0_, student0_.sex as sex5_1_0_, student0_.sname as sname6_1_0_, student0_.point as point7_1_0_ from student student0_ where student0_.studentno=? Hibernate: select clas0_.classno as classno1_0_0_, clas0_.department as departme2_0_0_, clas0_.monitor as monitor3_0_0_, clas0_.classname as classnam4_0_0_ from class clas0_ where clas0_.classno=? Hibernate: select students0_.classno as classno3_1_0_, students0_.studentno as studentn1_1_0_, students0_.studentno as studentn1_1_1_, students0_.birthday as birthday2_1_1_, students0_.classno as classno3_1_1_, students0_.phone as phone4_1_1_, students0_.sex as sex5_1_1_, students0_.sname as sname6_1_1_, students0_.point as point7_1_1_ from student students0_ where students0_.classno=? [Clas{classno=tx171, department=‘信工学院‘, monitor=‘张伟‘, classname=‘通信171‘, students=[Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}]}]
(6)lazy="true" fetch="subselect"
Hibernate: select clas0_.classno as classno1_0_, clas0_.department as departme2_0_, clas0_.monitor as monitor3_0_, clas0_.classname as classnam4_0_ from class clas0_ Hibernate: select students0_.classno as classno3_1_1_, students0_.studentno as studentn1_1_1_, students0_.studentno as studentn1_1_0_, students0_.birthday as birthday2_1_0_, students0_.classno as classno3_1_0_, students0_.phone as phone4_1_0_, students0_.sex as sex5_1_0_, students0_.sname as sname6_1_0_, students0_.point as point7_1_0_ from student students0_ where students0_.classno in ( select clas0_.classno from class clas0_ ) Clas{classno=80501, department=‘计算机学院‘, monitor=‘刘国平‘, classname=‘计算机0801‘, students=[]} [] 0 Clas{classno=80601, department=‘机械学院‘, monitor=‘王善执‘, classname=‘机械0801‘, students=[]} [] 0 Clas{classno=90501, department=‘计算机学院‘, monitor=‘马文斐‘, classname=‘计算机0901‘, students=[]} [] 0 Clas{classno=90502, department=‘计算机学院‘, monitor=‘章成楠‘, classname=‘计算机0902‘, students=[]} [] 0 Clas{classno=90801, department=‘管理学院‘, monitor=‘党海‘, classname=‘管理0901‘, students=[]} [] 0 Clas{classno=90802, department=‘管理学院‘, monitor=‘张晓‘, classname=‘管理0802‘, students=[]} [] 0 Clas{classno=tx161, department=‘信息工程学院‘, monitor=‘张丽‘, classname=‘通信161‘, students=[]} [] 0 Clas{classno=tx172, department=‘信工学院‘, monitor=‘李月‘, classname=‘通信172‘, students=[]} [] 0 Clas{classno=tx171, department=‘信工学院‘, monitor=‘张伟‘, classname=‘通信171‘, students=[Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}]} [Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}] 3
(7)lazy="false" fetch="subselect"
Hibernate: select clas0_.classno as classno1_0_, clas0_.department as departme2_0_, clas0_.monitor as monitor3_0_, clas0_.classname as classnam4_0_ from class clas0_ Hibernate: select students0_.classno as classno3_1_1_, students0_.studentno as studentn1_1_1_, students0_.studentno as studentn1_1_0_, students0_.birthday as birthday2_1_0_, students0_.classno as classno3_1_0_, students0_.phone as phone4_1_0_, students0_.sex as sex5_1_0_, students0_.sname as sname6_1_0_, students0_.point as point7_1_0_ from student students0_ where students0_.classno in ( select clas0_.classno from class clas0_ ) Clas{classno=80501, department=‘计算机学院‘, monitor=‘刘国平‘, classname=‘计算机0801‘, students=[]} [] 0 Clas{classno=80601, department=‘机械学院‘, monitor=‘王善执‘, classname=‘机械0801‘, students=[]} [] 0 Clas{classno=90501, department=‘计算机学院‘, monitor=‘马文斐‘, classname=‘计算机0901‘, students=[]} [] 0 Clas{classno=90502, department=‘计算机学院‘, monitor=‘章成楠‘, classname=‘计算机0902‘, students=[]} [] 0 Clas{classno=90801, department=‘管理学院‘, monitor=‘党海‘, classname=‘管理0901‘, students=[]} [] 0 Clas{classno=90802, department=‘管理学院‘, monitor=‘张晓‘, classname=‘管理0802‘, students=[]} [] 0 Clas{classno=tx161, department=‘信息工程学院‘, monitor=‘张丽‘, classname=‘通信161‘, students=[]} [] 0 Clas{classno=tx172, department=‘信工学院‘, monitor=‘李月‘, classname=‘通信172‘, students=[]} [] 0 Clas{classno=tx171, department=‘信工学院‘, monitor=‘张伟‘, classname=‘通信171‘, students=[Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}]} [Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}] 3
(8)lazy="extra" fetch="subselect"
Hibernate: select clas0_.classno as classno1_0_, clas0_.department as departme2_0_, clas0_.monitor as monitor3_0_, clas0_.classname as classnam4_0_ from class clas0_ Hibernate: select students0_.classno as classno3_1_1_, students0_.studentno as studentn1_1_1_, students0_.studentno as studentn1_1_0_, students0_.birthday as birthday2_1_0_, students0_.classno as classno3_1_0_, students0_.phone as phone4_1_0_, students0_.sex as sex5_1_0_, students0_.sname as sname6_1_0_, students0_.point as point7_1_0_ from student students0_ where students0_.classno in ( select clas0_.classno from class clas0_ ) Clas{classno=80501, department=‘计算机学院‘, monitor=‘刘国平‘, classname=‘计算机0801‘, students=[]} [] 0 Clas{classno=80601, department=‘机械学院‘, monitor=‘王善执‘, classname=‘机械0801‘, students=[]} [] 0 Clas{classno=90501, department=‘计算机学院‘, monitor=‘马文斐‘, classname=‘计算机0901‘, students=[]} [] 0 Clas{classno=90502, department=‘计算机学院‘, monitor=‘章成楠‘, classname=‘计算机0902‘, students=[]} [] 0 Clas{classno=90801, department=‘管理学院‘, monitor=‘党海‘, classname=‘管理0901‘, students=[]} [] 0 Clas{classno=90802, department=‘管理学院‘, monitor=‘张晓‘, classname=‘管理0802‘, students=[]} [] 0 Clas{classno=tx161, department=‘信息工程学院‘, monitor=‘张丽‘, classname=‘通信161‘, students=[]} [] 0 Clas{classno=tx172, department=‘信工学院‘, monitor=‘李月‘, classname=‘通信172‘, students=[]} [] 0 Clas{classno=tx171, department=‘信工学院‘, monitor=‘张伟‘, classname=‘通信171‘, students=[Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘},Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}]} [Student{studentno=‘201811‘, sname=‘zhai‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=890.0, phone=‘19837372532‘}, Student{studentno=‘201813‘, sname=‘zhai3‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=892.0, phone=‘19837372534‘}, Student{studentno=‘201812‘, sname=‘zhai2‘, sex=‘男‘, birthday=‘1998-11-11‘, classno=‘tx171‘, point=893.0, phone=‘19837372533‘}] 3
原文地址:https://www.cnblogs.com/zhai1997/p/11980053.html
时间: 2024-10-11 16:05:34