懒加载用于何时发出sql语句
抓取策略用于如何发出sql语句,
join连接形式的 当查出的一条的记录情况时候有效,子查询不起作用,select * from table1这是隐士子查询
还是 select n+1语句 还是 subselect子查询2条语句 select * from table1 where (select * in (table2))
在 <set>那里添加这两个属性。
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="cn.itcast.hiberate.sh.domain.Classes"> 6 <id name="cid" length="5" type="java.lang.Long"> 7 <generator class="increment"></generator> 8 </id> 9 <property name="cname" length="20" type="java.lang.String"></property> 10 11 <property name="description" length="100" type="java.lang.String"></property> 12 <!-- 13 set元素对应类中的set集合 14 通过set元素使classes表与student表建立关联 15 key是通过外键的形式让两张表建立关联 16 one-to-many是通过类的形式让两个类建立关联 17 18 cascade 级联 19 save-update 20 1、当 保存班级的时候,对学生进行怎么样的操作 21 如果学生对象在数据库中没有对应的值,这个时候会执行save操作 22 如果学生对象在数据库中有对应的值,这个时候会执行update操作 23 delete 24 all 25 inverse 维护关系 26 true 不维护关系 27 false 维护关系 28 default false 29 --> 30 <set name="students" cascade="save-update" inverse="true" lazy="false" fetch="join"> 31 <!-- 32 key是用来描述外键 33 --> 34 <key> 35 <column name="cid"></column> 36 </key> 37 <one-to-many class="cn.itcast.hiberate.sh.domain.Student"/> 38 </set> 39 </class> 40 </hibernate-mapping>
懒加载
1 package cn.itcast.hibernate.sh.test; 2 3 import java.util.Set; 4 5 import org.hibernate.Session; 6 import org.junit.Test; 7 8 import cn.itcast.hiberate.sh.domain.Classes; 9 import cn.itcast.hiberate.sh.domain.Student; 10 import cn.itcast.hibernate.sh.utils.HiberanteUtils; 11 12 public class LazyTest extends HiberanteUtils{ 13 static{ 14 url = "hibernate.cfg.xml"; 15 } 16 /** 17 * 类的懒加载 18 */ 19 @Test 20 public void testLoad(){ 21 Session session = sessionFactory.openSession(); 22 Classes classes = (Classes)session.load(Classes.class, 1L); 23 session.close(); 24 classes.getCname(); 25 } 26 27 /** 28 * 集合的延迟加载 29 */ 30 @Test 31 public void testSet(){ 32 Session session = sessionFactory.openSession(); 33 Classes classes = (Classes)session.get(Classes.class, 1L); 34 Set<Student> students = classes.getStudents(); 35 for(Student student:students){//这个时候才要发出sql语句 36 System.out.println(student.getSname()); 37 } 38 session.close(); 39 } 40 41 /** 42 * 集合的延迟加载 43 */ 44 @Test 45 public void testSet_EXTRA(){ 46 Session session = sessionFactory.openSession(); 47 Classes classes = (Classes)session.get(Classes.class, 1L); 48 Set<Student> students = classes.getStudents(); 49 System.out.println(students.size()); 50 session.close(); 51 } 52 }
抓取策略
1 package cn.itcast.hibernate.sh.test; 2 3 import java.util.List; 4 import java.util.Set; 5 6 import org.hibernate.Session; 7 import org.junit.Test; 8 9 import cn.itcast.hiberate.sh.domain.Classes; 10 import cn.itcast.hiberate.sh.domain.Student; 11 import cn.itcast.hibernate.sh.utils.HiberanteUtils; 12 13 public class FetchTest extends HiberanteUtils{ 14 static{ 15 url = "hibernate.cfg.xml"; 16 } 17 18 /** 19 * n+1的问题 20 * 解决问题的方案:子查询 fetch="subselect" 21 */ 22 @Test 23 public void testAll_Classes(){ 24 Session session = sessionFactory.openSession(); 25 List<Classes> cList = session.createQuery("from Classes").list(); 26 for(Classes classes:cList){ 27 Set<Student> students = classes.getStudents(); 28 for(Student student:students){ 29 System.out.println(student.getSname()); 30 } 31 } 32 session.close(); 33 } 34 35 /** 36 * n+1的问题 37 * 解决问题的方案:子查询 fetch="subselect" 38 */ 39 @Test 40 public void testClasses_Some(){ 41 Session session = sessionFactory.openSession(); 42 List<Classes> cList = session.createQuery("from Classes where cid in(1,2,3)").list(); 43 for(Classes classes:cList){ 44 Set<Student> students = classes.getStudents(); 45 for(Student student:students){ 46 System.out.println(student.getSname()); 47 } 48 } 49 session.close(); 50 } 51 52 @Test 53 public void testQueryClasses_Id(){ 54 Session session = sessionFactory.openSession(); 55 Classes classes = (Classes)session.get(Classes.class, 1L); 56 Set<Student> students = classes.getStudents(); 57 for(Student student:students){ 58 System.out.println(student.getSname()); 59 } 60 session.close(); 61 } 62 }