batch-size属性,可以批量加载实体类或集合,例如:
例子代码如下:
Student.hbm.xml <hibernate-mapping package="hibernate"> <class name="Classes" table="t_classes" batch-size="3"> <id name="id"> <generator class="native"/> id> <property name="name"/> <set name="students" inverse="true" cascade="all"> <key column="classesid"/> <one-to-many class="Student"/> set> class> hibernate-mapping> Classes.hbm.xml <hibernate-mapping package="hibernate"> <class name="Classes" table="t_classes"> <id name="id"> <generator class="native"/> id> <property name="name"/> <set name="students" inverse="true" cascade="all" batch-size="5"> <key column="classesid"/> <one-to-many class="Student"/> set> class> hibernate-mapping> 测试代码: public class FechTest extends TestCase { public void testFetch1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction();
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()); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testFetch1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction();
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()); } } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } } |