(九)Hibernate 检索策略

所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java

这里的hibernate.cfg.xml配置信息我就不再写了

第一节:检索策略属性Lazy

 

Lazy:true (默认) 延迟检索;set 端一对多

Lazy:false 立即检索;set 端一对多

Lazy:extra 增强延迟检索; set 端一对多

Lazy:proxy(默认) 延迟检索;many-to-one 多对一

Lazy:no-proxy 无代理延迟检索;many-to-one 多对一(需要编译时字节码增强)

 

 
Lazy:true (默认) 延迟检索;set 端一对多

 Class.java

 1 package com.wishwzp.model;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Class {
 7 
 8     private long id;
 9     private String name;
10     private Set<Student> students=new HashSet<Student>();
11     
12     public long getId() {
13         return id;
14     }
15     public void setId(long id) {
16         this.id = id;
17     }
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24     public Set<Student> getStudents() {
25         return students;
26     }
27     public void setStudents(Set<Student> students) {
28         this.students = students;
29     }
30     
31 }

 

Student.java

 1 package com.wishwzp.model;
 2 
 3 public class Student {
 4 
 5     private long id;
 6     private String name;
 7     private Class c;
 8     
 9     public long getId() {
10         return id;
11     }
12     public void setId(long id) {
13         this.id = id;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     
22     
23     public Class getC() {
24         return c;
25     }
26     public void setC(Class c) {
27         this.c = c;
28     }
29     @Override
30     public String toString() {
31         return "Student [id=" + id + ", name=" + name + "]";
32     }
33 }

 

 Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="true">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="stuName"></property>
14         
15         <many-to-one name="c" column="classId" class="com.wishwzp.model.Class" cascade="save-update"></many-to-one>
16     </class>
17 
18 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34 
35     @Test
36     public void testLazy1() {
37         Class c=(Class)session.get(Class.class, Long.valueOf(1));
38         Set<Student> studentList=(Set<Student>)c.getStudents();
39         studentList.iterator();
40     }
41 }

 

数据库里面的信息数据:

运行显示结果:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?
Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=?

 

 

Lazy:false 立即检索;set 端一对多

Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="false">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34 
35     @Test
36     public void testLazy1() {
37         Class c=(Class)session.get(Class.class, Long.valueOf(1));
38         
39     }
40     
41 }

 

还是上面第一个的数据库信息。

运行显示结果:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?
Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=?

 

Lazy:extra 增强延迟检索; set 端一对多

Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="extra">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34 
35     @Test
36     public void testLazy1() {
37         Class c=(Class)session.get(Class.class, Long.valueOf(1));
38         Set<Student> studentList=(Set<Student>)c.getStudents();
39         System.out.println(studentList.size());
40     }
41     
42 }

 

还是上面第一个的数据库信息。

运行结果显示:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?
Hibernate: select count(stuId) from t_student where classId =?
3

 

Lazy:proxy(默认) 延迟检索;many-to-one 多对一

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="stuName"></property>
14         
15         <many-to-one name="c" column="classId" class="com.wishwzp.model.Class" cascade="save-update" lazy="proxy"></many-to-one>
16     </class>
17 
18 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34 
35     
36     @Test
37     public void testLazy2() {
38         Student student=(Student)session.get(Student.class, Long.valueOf(1));
39         student.getC().getName();
40     }
41     
42 
43 }

 

运行显示结果:

Hibernate: select student0_.stuId as stuId1_1_0_, student0_.stuName as stuName2_1_0_, student0_.classId as classId3_1_0_ from t_student student0_ where student0_.stuId=?
Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=?

 

Lazy:no-proxy 无代理延迟检索;many-to-one 多对一(需要编译时字节码增强)

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="stuName"></property>
14         
15         <many-to-one name="c" column="classId" class="com.wishwzp.model.Class" cascade="save-update" lazy="no-proxy"></many-to-one>
16     </class>
17 
18 </hibernate-mapping>

 

StudentTest.java和上面一样

 

 

 第二节:检索策略属性batch-size

 

1,批量延迟检索;

2,批量立即检索;

 

1,批量延迟检索;

Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="true" batch-size="3">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34     
35     @Test
36     public void testBatch1(){
37         List<Class> classList=session.createQuery("from Class").list();
38         Iterator it=classList.iterator();
39         Class c1=(Class)it.next();
40         Class c2=(Class)it.next();
41         Class c3=(Class)it.next();
42         c1.getStudents().iterator();
43         c2.getStudents().iterator();
44         c3.getStudents().iterator();
45     }
46     
47 }

 

数据库多加了一条信息:

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?, ?)

 

2,批量立即检索;

Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="3">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34     
35     @Test
36     public void testBatch2(){
37         List<Class> classList=session.createQuery("from Class").list();
38         
39     }
40     
41 }

 

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?, ?)

 

假如我们将Class.hbm.xml的batch-size="3"改成batch-size="2"的话,运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?)
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId=?

 

 

第三节:检索策略属性Fetch

 

1,Fetch:select(默认) 查询方式;

2,Fetch:subselect 子查询方式;

3,Fetch:join 迫切左外连接查询方式;

 

1,Fetch:select(默认) 查询方式;

Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="2" fetch="select">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34     
35     @Test
36     public void testFetch1(){
37         List<Class> classList=session.createQuery("from Class").list();
38         Iterator it=classList.iterator();
39         Class c1=(Class)it.next();
40         Class c2=(Class)it.next();
41         Class c3=(Class)it.next();
42         c1.getStudents().iterator();
43         c2.getStudents().iterator();
44         c3.getStudents().iterator();
45     }
46     
47 }

 

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (?, ?)
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId=?

 

 

2,Fetch:subselect 子查询方式;

Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="2" fetch="subselect">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34 
35     
36     @Test
37     public void testFetch1(){
38         List<Class> classList=session.createQuery("from Class").list();
39         Iterator it=classList.iterator();
40         Class c1=(Class)it.next();
41         Class c2=(Class)it.next();
42         Class c3=(Class)it.next();
43         c1.getStudents().iterator();
44         c2.getStudents().iterator();
45         c3.getStudents().iterator();
46     }
47     
48 }

 

运行结果显示:

Hibernate: select class0_.classId as classId1_0_, class0_.className as classNam2_0_ from t_class class0_
Hibernate: select students0_.classId as classId3_0_1_, students0_.stuId as stuId1_1_1_, students0_.stuId as stuId1_1_0_, students0_.stuName as stuName2_1_0_, students0_.classId as classId3_1_0_ from t_student students0_ where students0_.classId in (select class0_.classId from t_class class0_)

 

3,Fetch:join 迫切左外连接查询方式;

Class.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Class" table="t_class">
 9         <id name="id" column="classId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="className"></property>
14         
15         <set name="students" cascade="delete" inverse="true" lazy="false" batch-size="2" fetch="join">
16             <key column="classId"></key>
17             <one-to-many class="com.wishwzp.model.Student"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Class;
15 import com.wishwzp.model.Student;
16 import com.wishwzp.util.HibernateUtil;
17 
18 public class StudentTest {
19 
20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
21     private Session session;
22     
23     @Before
24     public void setUp() throws Exception {
25         session=sessionFactory.openSession(); // 生成一个session
26         session.beginTransaction(); // 开启事务
27     }
28 
29     @After
30     public void tearDown() throws Exception {
31          session.getTransaction().commit(); // 提交事务
32          session.close(); // 关闭session
33     }
34     
35     @Test
36     public void testFetch2(){
37         Class c=(Class)session.get(Class.class, Long.valueOf(1));
38     }
39 }

 

运行结果显示:

Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_, students1_.classId as classId3_0_1_, students1_.stuId as stuId1_1_1_, students1_.stuId as stuId1_1_2_, students1_.stuName as stuName2_1_2_, students1_.classId as classId3_1_2_ from t_class class0_ left outer join t_student students1_ on class0_.classId=students1_.classId where class0_.classId=?

 

 


 

END

 

posted @ 2016-05-17 23:30  kika  阅读(208)  评论(0编辑  收藏  举报