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>