hibernate的抓取策略

hibernate抓取策略(单端代理的批量抓取)

保持默认,同fetch="select",如:

<many-to-onename="classes"column="classesid"fetch="select"/>

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合
//fetch1,fetch2
	public void testFetch1() {
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			session.beginTransaction();
			
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			System.out.println("classes.name=" + student.getClasses().getName());
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateSessionFactory.closeSession(session);
		}
	}

设置fetch="join",如:

<many-to-onename="classes"column="classesid"fetch="join"/>

fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合

此时lazy会失效,同上

保持默认,同fetch="select",如:

<setname="students"inverse="true"cascade="all"fetch="select">

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合
//fetch3,fetch4
	public void testFetch2() {
		Session session = null;
		try {
			session = HibernateSessionFactory.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 {
			HibernateSessionFactory.closeSession(session);
		}
	}

设置fetch="join",如:

<setname="students"inverse="true"cascade="all"fetch="join">

fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合

此时lazy会失效

设置fetch="subselect",如:

<setname="students"inverse="true"cascade="all"fetch="subselect">

fetch="subselect",另外发送一条select语句抓取在前面查询到的所有实体对象的关联集合
//fetch5
	public void testFetch3() {
		Session session = null;
		try {
			session = HibernateSessionFactory.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 {
			HibernateSessionFactory.closeSession(session);
		}
	}

batch-size属性,可以批量加载实体类,参见:Classes.hbm.xml<class name="Classes" table="t_classes" batch-size="3">

List students = session.createQuery("select s from Student s where s.id in(:ids)")
.setParameterList("ids", new Object[]{1, 11, 21, 31, 41, 51, 61, 71, 81, 91})
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
	Student  student = (Student)iter.next();
	System.out.println("student.name=" + student.getName());
	System.out.println("classes.name=" + student.getClasses().getName());
}

batch-size属性,可以批量加载实体类,参见:Classes.hbm.xml

<set name="students" inverse="true" cascade="all" batch-size="5">
List classesList = session.createQuery("select c from Classes c").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());
	}
}

相关推荐