hibernate在集合上的lazy策略,可以取值:true/false/extra
<class>标签上的lazy不会影响到集合上的lazy特性
例子:
Classes.hbm.xml
1<?xml version="1.0"?>
2<!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5<hibernate-mapping package="com.bjsxt.hibernate">
6 <class name="Classes" table="t_classes">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 <set name="students" inverse="true" cascade="all" lazy="extra">
12 <key column="classesid"/>
13 <one-to-many class="Student"/>
14 </set>
15 </class>
16</hibernate-mapping>
1<?xml version="1.0"?>
2<!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5<hibernate-mapping package="com.bjsxt.hibernate">
6 <class name="Classes" table="t_classes">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 <set name="students" inverse="true" cascade="all" lazy="extra">
12 <key column="classesid"/>
13 <one-to-many class="Student"/>
14 </set>
15 </class>
16</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://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5<hibernate-mapping>
6 <class name="com.bjsxt.hibernate.Student" table="t_student">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 <many-to-one name="classes" column="classesid"/>
12 </class>
13</hibernate-mapping>
1<?xml version="1.0"?>
2<!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5<hibernate-mapping>
6 <class name="com.bjsxt.hibernate.Student" table="t_student">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 <many-to-one name="classes" column="classesid"/>
12 </class>
13</hibernate-mapping>
CellectionlazyTest1.java
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 保持lazy默认
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest1 extends TestCase {
17
18
19 public void testLoad1() {
20 Session session = null;
21 try {
22 session = HibernateUtils.getSession();
23 session.beginTransaction();
24
25 //不会发出sql
26 Classes classes = (Classes)session.load(Classes.class, 1);
27
28 //会发出sql
29 System.out.println("classes.name=" + classes.getName());
30
31 //不会发出sql
32 Set students = classes.getStudents();
33
34 //会发出sql
35 for (Iterator iter=students.iterator(); iter.hasNext();) {
36 Student student = (Student)iter.next();
37 System.out.println("student.name=" + student.getName());
38 }
39 session.getTransaction().commit();
40 }catch(Exception e) {
41 e.printStackTrace();
42 session.getTransaction().rollback();
43 }finally {
44 HibernateUtils.closeSession(session);
45 }
46 }
47
48 public void testLoad2() {
49 Session session = null;
50 try {
51 session = HibernateUtils.getSession();
52 session.beginTransaction();
53
54 //不会发出sql
55 Classes classes = (Classes)session.load(Classes.class, 1);
56
57 //会发出sql
58 System.out.println("classes.name=" + classes.getName());
59
60 //不会发出sql
61 Set students = classes.getStudents();
62
63 //会发出sql,发出查询全部数据的sql
64 System.out.println("student.count=" + students.size());
65 session.getTransaction().commit();
66 }catch(Exception e) {
67 e.printStackTrace();
68 session.getTransaction().rollback();
69 }finally {
70 HibernateUtils.closeSession(session);
71 }
72 }
73}
74
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 保持lazy默认
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest1 extends TestCase {
17
18
19 public void testLoad1() {
20 Session session = null;
21 try {
22 session = HibernateUtils.getSession();
23 session.beginTransaction();
24
25 //不会发出sql
26 Classes classes = (Classes)session.load(Classes.class, 1);
27
28 //会发出sql
29 System.out.println("classes.name=" + classes.getName());
30
31 //不会发出sql
32 Set students = classes.getStudents();
33
34 //会发出sql
35 for (Iterator iter=students.iterator(); iter.hasNext();) {
36 Student student = (Student)iter.next();
37 System.out.println("student.name=" + student.getName());
38 }
39 session.getTransaction().commit();
40 }catch(Exception e) {
41 e.printStackTrace();
42 session.getTransaction().rollback();
43 }finally {
44 HibernateUtils.closeSession(session);
45 }
46 }
47
48 public void testLoad2() {
49 Session session = null;
50 try {
51 session = HibernateUtils.getSession();
52 session.beginTransaction();
53
54 //不会发出sql
55 Classes classes = (Classes)session.load(Classes.class, 1);
56
57 //会发出sql
58 System.out.println("classes.name=" + classes.getName());
59
60 //不会发出sql
61 Set students = classes.getStudents();
62
63 //会发出sql,发出查询全部数据的sql
64 System.out.println("student.count=" + students.size());
65 session.getTransaction().commit();
66 }catch(Exception e) {
67 e.printStackTrace();
68 session.getTransaction().rollback();
69 }finally {
70 HibernateUtils.closeSession(session);
71 }
72 }
73}
74
CellectionlazyTest2.java
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 设置<class>标签上的lazy=false
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest2 extends TestCase {
17
18
19 public void testLoad1() {
20 Session session = null;
21 try {
22 session = HibernateUtils.getSession();
23 session.beginTransaction();
24
25 //会发出sql
26 Classes classes = (Classes)session.load(Classes.class, 1);
27
28 //不会发出sql
29 System.out.println("classes.name=" + classes.getName());
30
31 //不会发出sql
32 Set students = classes.getStudents();
33
34 //会发出sql
35 for (Iterator iter=students.iterator(); iter.hasNext();) {
36 Student student = (Student)iter.next();
37 System.out.println("student.name=" + student.getName());
38 }
39 session.getTransaction().commit();
40 }catch(Exception e) {
41 e.printStackTrace();
42 session.getTransaction().rollback();
43 }finally {
44 HibernateUtils.closeSession(session);
45 }
46 }
47}
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 设置<class>标签上的lazy=false
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest2 extends TestCase {
17
18
19 public void testLoad1() {
20 Session session = null;
21 try {
22 session = HibernateUtils.getSession();
23 session.beginTransaction();
24
25 //会发出sql
26 Classes classes = (Classes)session.load(Classes.class, 1);
27
28 //不会发出sql
29 System.out.println("classes.name=" + classes.getName());
30
31 //不会发出sql
32 Set students = classes.getStudents();
33
34 //会发出sql
35 for (Iterator iter=students.iterator(); iter.hasNext();) {
36 Student student = (Student)iter.next();
37 System.out.println("student.name=" + student.getName());
38 }
39 session.getTransaction().commit();
40 }catch(Exception e) {
41 e.printStackTrace();
42 session.getTransaction().rollback();
43 }finally {
44 HibernateUtils.closeSession(session);
45 }
46 }
47}
CellectionlazyTest3.java
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 设置集合上的lazy=false,其它默认
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest3 extends TestCase {
17
18
19 public void testLoad1() {
20 Session session = null;
21 try {
22 session = HibernateUtils.getSession();
23 session.beginTransaction();
24
25 //不会发出sql
26 Classes classes = (Classes)session.load(Classes.class, 1);
27
28 //会发出sql,会发出两条sql分别加载Classes和Student
29 System.out.println("classes.name=" + classes.getName());
30
31 //不会发出sql
32 Set students = classes.getStudents();
33
34 //不会发出sql
35 for (Iterator iter=students.iterator(); iter.hasNext();) {
36 Student student = (Student)iter.next();
37 System.out.println("student.name=" + student.getName());
38 }
39 session.getTransaction().commit();
40 }catch(Exception e) {
41 e.printStackTrace();
42 session.getTransaction().rollback();
43 }finally {
44 HibernateUtils.closeSession(session);
45 }
46 }
47
48 public void testLoad2() {
49 Session session = null;
50 try {
51 session = HibernateUtils.getSession();
52 session.beginTransaction();
53
54 //不会发出sql
55 Classes classes = (Classes)session.load(Classes.class, 1);
56
57 //会发出sql,会发出两条sql分别加载Classes和Student
58 System.out.println("classes.name=" + classes.getName());
59
60 //不会发出sql
61 Set students = classes.getStudents();
62
63 //不会发出sql
64 System.out.println("student.count=" + students.size());
65 session.getTransaction().commit();
66 }catch(Exception e) {
67 e.printStackTrace();
68 session.getTransaction().rollback();
69 }finally {
70 HibernateUtils.closeSession(session);
71 }
72 }
73}
74
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 设置集合上的lazy=false,其它默认
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest3 extends TestCase {
17
18
19 public void testLoad1() {
20 Session session = null;
21 try {
22 session = HibernateUtils.getSession();
23 session.beginTransaction();
24
25 //不会发出sql
26 Classes classes = (Classes)session.load(Classes.class, 1);
27
28 //会发出sql,会发出两条sql分别加载Classes和Student
29 System.out.println("classes.name=" + classes.getName());
30
31 //不会发出sql
32 Set students = classes.getStudents();
33
34 //不会发出sql
35 for (Iterator iter=students.iterator(); iter.hasNext();) {
36 Student student = (Student)iter.next();
37 System.out.println("student.name=" + student.getName());
38 }
39 session.getTransaction().commit();
40 }catch(Exception e) {
41 e.printStackTrace();
42 session.getTransaction().rollback();
43 }finally {
44 HibernateUtils.closeSession(session);
45 }
46 }
47
48 public void testLoad2() {
49 Session session = null;
50 try {
51 session = HibernateUtils.getSession();
52 session.beginTransaction();
53
54 //不会发出sql
55 Classes classes = (Classes)session.load(Classes.class, 1);
56
57 //会发出sql,会发出两条sql分别加载Classes和Student
58 System.out.println("classes.name=" + classes.getName());
59
60 //不会发出sql
61 Set students = classes.getStudents();
62
63 //不会发出sql
64 System.out.println("student.count=" + students.size());
65 session.getTransaction().commit();
66 }catch(Exception e) {
67 e.printStackTrace();
68 session.getTransaction().rollback();
69 }finally {
70 HibernateUtils.closeSession(session);
71 }
72 }
73}
74
CellectionlazyTest4.java
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 设置集合上的lazy=extra,其它默认
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest4 extends TestCase {
17
18 public void testLoad1() {
19 Session session = null;
20 try {
21 session = HibernateUtils.getSession();
22 session.beginTransaction();
23
24 //不会发出sql
25 Classes classes = (Classes)session.load(Classes.class, 1);
26
27 //会发出sql
28 System.out.println("classes.name=" + classes.getName());
29
30 //不会发出sql
31 Set students = classes.getStudents();
32
33 //会发出sql
34 for (Iterator iter=students.iterator(); iter.hasNext();) {
35 Student student = (Student)iter.next();
36 System.out.println("student.name=" + student.getName());
37 }
38 session.getTransaction().commit();
39 }catch(Exception e) {
40 e.printStackTrace();
41 session.getTransaction().rollback();
42 }finally {
43 HibernateUtils.closeSession(session);
44 }
45 }
46
47 public void testLoad2() {
48 Session session = null;
49 try {
50 session = HibernateUtils.getSession();
51 session.beginTransaction();
52
53 //不会发出sql
54 Classes classes = (Classes)session.load(Classes.class, 1);
55
56 //会发出sql
57 System.out.println("classes.name=" + classes.getName());
58
59 //不会发出sql
60 Set students = classes.getStudents();
61 //会发出sql,发出一条比较智能的sql
62 System.out.println("student.count=" + students.size());
63 session.getTransaction().commit();
64 }catch(Exception e) {
65 e.printStackTrace();
66 session.getTransaction().rollback();
67 }finally {
68 HibernateUtils.closeSession(session);
69 }
70 }
71}
1package com.bjsxt.hibernate;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import junit.framework.TestCase;
8
9import org.hibernate.Session;
10
11/** *//**
12 * 设置集合上的lazy=extra,其它默认
13 * @author Administrator
14 *
15 */
16public class CellectionlazyTest4 extends TestCase {
17
18 public void testLoad1() {
19 Session session = null;
20 try {
21 session = HibernateUtils.getSession();
22 session.beginTransaction();
23
24 //不会发出sql
25 Classes classes = (Classes)session.load(Classes.class, 1);
26
27 //会发出sql
28 System.out.println("classes.name=" + classes.getName());
29
30 //不会发出sql
31 Set students = classes.getStudents();
32
33 //会发出sql
34 for (Iterator iter=students.iterator(); iter.hasNext();) {
35 Student student = (Student)iter.next();
36 System.out.println("student.name=" + student.getName());
37 }
38 session.getTransaction().commit();
39 }catch(Exception e) {
40 e.printStackTrace();
41 session.getTransaction().rollback();
42 }finally {
43 HibernateUtils.closeSession(session);
44 }
45 }
46
47 public void testLoad2() {
48 Session session = null;
49 try {
50 session = HibernateUtils.getSession();
51 session.beginTransaction();
52
53 //不会发出sql
54 Classes classes = (Classes)session.load(Classes.class, 1);
55
56 //会发出sql
57 System.out.println("classes.name=" + classes.getName());
58
59 //不会发出sql
60 Set students = classes.getStudents();
61 //会发出sql,发出一条比较智能的sql
62 System.out.println("student.count=" + students.size());
63 session.getTransaction().commit();
64 }catch(Exception e) {
65 e.printStackTrace();
66 session.getTransaction().rollback();
67 }finally {
68 HibernateUtils.closeSession(session);
69 }
70 }
71}