34hibernate_fetch_5_set_subselect
hibernate抓取策略(集合代理的批量抓取)
设置fetch="subselect",如:
<set name="students" inverse="true" cascade="all" fetch="subselect">
fetch="subselect",另外发送一条select语句抓取在前面查询到的所有实体对象的关联集合
============================================================================
testFetch1:
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Classes classes = (Classes)session.load(Classes.class, 1);
System.out.println("classes.name=" + classes.getName());
for (Iterator iter=classes.getStudents().iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("student.name=" + student.getName());
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级0
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级0的学生6
student.name=班级0的学生1
student.name=班级0的学生5
student.name=班级0的学生4
student.name=班级0的学生0
student.name=班级0的学生2
student.name=班级0的学生3
====================================影响hql查询=======================================
testFetch2:
/**
* Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_,
* students0_.id as id1_0_, students0_.name as name1_0_,
* students0_.classesid as classesid1_0_
* from t_student students0_
* where students0_.classesid in (select classes0_.id from t_classes classes0_ where classes0_.id in (1 , 2 , 3))
*/
public void testFetch2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List classesList = session.createQuery("select c from Classes c where c.id in(1, 2, 3)").list();
for (Iterator iter=classesList.iterator(); iter.hasNext();) {
Classes classes = (Classes)iter.next();
System.out.println("classes.name=" + classes.getName());
for (Iterator iter1=classes.getStudents().iterator(); iter1.hasNext();) {
Student student = (Student)iter1.next();
System.out.println("student.name=" + student.getName());
}
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
设置:<set name="students" inverse="true" cascade="all">
Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from t_classes classes0_ where classes0_.id in (1 , 2 , 3)
classes.name=班级0
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级0的学生2
student.name=班级0的学生1
student.name=班级0的学生6
student.name=班级0的学生5
student.name=班级0的学生4
student.name=班级0的学生3
student.name=班级0的学生0
classes.name=班级1
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级1的学生2
student.name=班级1的学生0
student.name=班级1的学生5
student.name=班级1的学生6
student.name=班级1的学生4
student.name=班级1的学生1
student.name=班级1的学生3
classes.name=班级2
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级2的学生2
student.name=班级2的学生0
student.name=班级2的学生3
student.name=班级2的学生1
student.name=班级2的学生4
student.name=班级2的学生6
student.name=班级2的学生5
设置:<set name="students" inverse="true" cascade="all" fetch="subselect">
Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from t_classes classes0_ where classes0_.id in (1 , 2 , 3)
classes.name=班级0
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid in (select classes0_.id from t_classes classes0_ where classes0_.id in (1 , 2 , 3))
student.name=班级0的学生2
student.name=班级0的学生1
student.name=班级0的学生5
student.name=班级0的学生6
student.name=班级0的学生4
student.name=班级0的学生0
student.name=班级0的学生3
classes.name=班级1
student.name=班级1的学生4
student.name=班级1的学生5
student.name=班级1的学生3
student.name=班级1的学生2
student.name=班级1的学生6
student.name=班级1的学生0
student.name=班级1的学生1
classes.name=班级2
student.name=班级2的学生5
student.name=班级2的学生4
student.name=班级2的学生0
student.name=班级2的学生3
student.name=班级2的学生1
student.name=班级2的学生6
student.name=班级2的学生2
设置fetch="subselect",如:
<set name="students" inverse="true" cascade="all" fetch="subselect">
fetch="subselect",另外发送一条select语句抓取在前面查询到的所有实体对象的关联集合
============================================================================
testFetch1:
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Classes classes = (Classes)session.load(Classes.class, 1);
System.out.println("classes.name=" + classes.getName());
for (Iterator iter=classes.getStudents().iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("student.name=" + student.getName());
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级0
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级0的学生6
student.name=班级0的学生1
student.name=班级0的学生5
student.name=班级0的学生4
student.name=班级0的学生0
student.name=班级0的学生2
student.name=班级0的学生3
====================================影响hql查询=======================================
testFetch2:
/**
* Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_,
* students0_.id as id1_0_, students0_.name as name1_0_,
* students0_.classesid as classesid1_0_
* from t_student students0_
* where students0_.classesid in (select classes0_.id from t_classes classes0_ where classes0_.id in (1 , 2 , 3))
*/
public void testFetch2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List classesList = session.createQuery("select c from Classes c where c.id in(1, 2, 3)").list();
for (Iterator iter=classesList.iterator(); iter.hasNext();) {
Classes classes = (Classes)iter.next();
System.out.println("classes.name=" + classes.getName());
for (Iterator iter1=classes.getStudents().iterator(); iter1.hasNext();) {
Student student = (Student)iter1.next();
System.out.println("student.name=" + student.getName());
}
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
设置:<set name="students" inverse="true" cascade="all">
Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from t_classes classes0_ where classes0_.id in (1 , 2 , 3)
classes.name=班级0
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级0的学生2
student.name=班级0的学生1
student.name=班级0的学生6
student.name=班级0的学生5
student.name=班级0的学生4
student.name=班级0的学生3
student.name=班级0的学生0
classes.name=班级1
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级1的学生2
student.name=班级1的学生0
student.name=班级1的学生5
student.name=班级1的学生6
student.name=班级1的学生4
student.name=班级1的学生1
student.name=班级1的学生3
classes.name=班级2
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid=?
student.name=班级2的学生2
student.name=班级2的学生0
student.name=班级2的学生3
student.name=班级2的学生1
student.name=班级2的学生4
student.name=班级2的学生6
student.name=班级2的学生5
设置:<set name="students" inverse="true" cascade="all" fetch="subselect">
Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from t_classes classes0_ where classes0_.id in (1 , 2 , 3)
classes.name=班级0
Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.classesid as classesid1_0_ from t_student students0_ where students0_.classesid in (select classes0_.id from t_classes classes0_ where classes0_.id in (1 , 2 , 3))
student.name=班级0的学生2
student.name=班级0的学生1
student.name=班级0的学生5
student.name=班级0的学生6
student.name=班级0的学生4
student.name=班级0的学生0
student.name=班级0的学生3
classes.name=班级1
student.name=班级1的学生4
student.name=班级1的学生5
student.name=班级1的学生3
student.name=班级1的学生2
student.name=班级1的学生6
student.name=班级1的学生0
student.name=班级1的学生1
classes.name=班级2
student.name=班级2的学生5
student.name=班级2的学生4
student.name=班级2的学生0
student.name=班级2的学生3
student.name=班级2的学生1
student.name=班级2的学生6
student.name=班级2的学生2