package cn.itcast.h3.query.hql; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import cn.itcast.h3.query.hql.vo.TeacherModel; import cn.itcast.util.HibernateUtil; public class MainRelaApp { //fetch="select" lazy="false" void selectFalse(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents().size()); System.out.println(queryList.get(0).getStudents()); s.close(); } //fetch="select" lazy="true" void selectTrue(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents()); System.out.println(queryList.get(0).getStudents().size()); s.close(); } //fetch="select" lazy="extra" void selectExtra(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents()); System.out.println(queryList.get(0).getStudents().size()); s.close(); } //fetch="subselect" lazy="false" void subselectFlase(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents()); System.out.println(queryList.get(0).getStudents().size()); s.close(); } //fetch="subselect" lazy="true" void subselectTrue(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); // System.out.println(queryList.get(0).getStudents().size()); System.out.println(queryList.get(0).getStudents()); s.close(); } //fetch="subselect" lazy="extra" void subselectExtra(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents().size()); System.out.println(queryList.get(0).getStudents()); s.close(); } //fetch="subselect" lazy="extra" Query-》OID load get void subselectExtra2(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel where uuid = 1"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents().size()); System.out.println(queryList.get(0).getStudents()); s.close(); } //fetch="join" lazy="false" void joinFalse(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents().size()); System.out.println(queryList.get(0).getStudents()); s.close(); } //fetch="join" lazy="true" void joinTrue(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents().size()); System.out.println(queryList.get(0).getStudents()); s.close(); } //fetch="join" lazy="extra" void joinExtra(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); System.out.println(queryList.get(0).getStudents().size()); System.out.println(queryList.get(0).getStudents()); s.close(); } //fetch="join" lazy="extra" get OID void joinExtra2(){ Session s = HibernateUtil.getSession(); TeacherModel tm = (TeacherModel) s.get(TeacherModel.class, 2L); System.out.println(tm.getStudents().size()); System.out.println(tm.getStudents()); s.close(); } //批量抓取策略 void batchQuery(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from TeacherModel"); List<TeacherModel> queryList = q.list(); // System.out.println(queryList.get(0).getStudents().size()); // System.out.println(queryList.get(0).getStudents()); s.close(); } public static void main(String[] args) { new MainRelaApp().batchQuery(); } }
package cn.itcast.h3.query.hql; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import cn.itcast.h3.query.hql.vo.StudentModel; import cn.itcast.util.HibernateUtil; //从关联数据检索策略 public class SubRelaApp { //fetch="select" lazy="false" void selectFlase(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from StudentModel"); List<StudentModel> queryList = q.list(); System.out.println(queryList.get(0).getTeacher()); s.close(); } //fetch="select" lazy="proxy" void selectProxy(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from StudentModel"); List<StudentModel> queryList = q.list(); // System.out.println(queryList.get(0).getTeacher()); // System.out.println(queryList.get(11).getTeacher()); // System.out.println(queryList.get(21).getTeacher()); s.close(); } //fetch="join" lazy="false" void joinFalse(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from StudentModel"); List<StudentModel> queryList = q.list(); System.out.println(queryList.get(0).getTeacher()); System.out.println(queryList.get(11).getTeacher()); System.out.println(queryList.get(21).getTeacher()); s.close(); } //fetch="join" lazy="proxy" void joinProxy(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from StudentModel"); List<StudentModel> queryList = q.list(); System.out.println(queryList.get(0).getTeacher()); System.out.println(queryList.get(11).getTeacher()); System.out.println(queryList.get(21).getTeacher()); s.close(); } //fetch="join" lazy="proxy" 使用OID查询 void joinProxy2(){ Session s = HibernateUtil.getSession(); StudentModel sm = (StudentModel) s.get(StudentModel.class, 4L); System.out.println(sm.getTeacher()); s.close(); } //批量加载 void batchQuery(){ Session s = HibernateUtil.getSession(); Query q = s.createQuery("from StudentModel"); List<StudentModel> queryList = q.list(); // System.out.println(queryList.get(0).getTeacher()); // System.out.println(queryList.get(11).getTeacher()); // System.out.println(queryList.get(21).getTeacher()); s.close(); } public static void main(String[] args) { new SubRelaApp().batchQuery(); } }
TeacherModel.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> <hibernate-mapping> <class name="cn.itcast.h3.query.hql.vo.TeacherModel" table="tbl_teacher" lazy="false"> <id name="uuid" column="uuid"> <generator class="native" /> </id> <property name="teacherName"/> <property name="nick"/> <set fetch="select" lazy="false" batch-size="5" name="students" > <key column="teacherUuid"/> <one-to-many class="cn.itcast.h3.query.hql.vo.StudentModel"/> </set> </class> </hibernate-mapping>
--------------------迷人的分割线--------------------
作者:架构师小跟班
开发者导航: http://www.codernav.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出,以免更多的人被误导。
作者:架构师小跟班
开发者导航: http://www.codernav.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出,以免更多的人被误导。