hibernate在单端关联上的lazy策略,可以取值:false/proxy/noproxy
<class>标签上的lazy不会影响到单端关联上的lazy特性
例子:
User.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.User" table="t_user" lazy="false">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 <!--
12 <many-to-one name="group" column="groupid" cascade="all"/>
13 -->
14 <many-to-one name="group" column="groupid"/>
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>
6 <class name="com.bjsxt.hibernate.User" table="t_user" lazy="false">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 <!--
12 <many-to-one name="group" column="groupid" cascade="all"/>
13 -->
14 <many-to-one name="group" column="groupid"/>
15 </class>
16</hibernate-mapping>
Group.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.Group" table="t_group">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 </class>
12</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.Group" table="t_group">
7 <id name="id">
8 <generator class="native"/>
9 </id>
10 <property name="name"/>
11 </class>
12</hibernate-mapping>
SingleEndTest1.java
1package com.bjsxt.hibernate;
2
3import org.hibernate.Session;
4
5import junit.framework.TestCase;
6
7/** *//**
8 * 所有lazy属性默认
9 * @author Administrator
10 *
11 */
12public class SingleEndTest1 extends TestCase {
13
14 public void testLoad1() {
15 Session session = null;
16 try {
17 session = HibernateUtils.getSession();
18 session.beginTransaction();
19
20 //不会发出sql
21 User user = (User)session.load(User.class, 1);
22
23 //会发出sql
24 System.out.println("user.name=" + user.getName());
25
26 //不会发出sql
27 Group group = user.getGroup();
28
29 //会发出sql
30 System.out.println("group.name=" + group.getName());
31
32 session.getTransaction().commit();
33 }catch(Exception e) {
34 e.printStackTrace();
35 session.getTransaction().rollback();
36 }finally {
37 HibernateUtils.closeSession(session);
38 }
39 }
40
41}
42
1package com.bjsxt.hibernate;
2
3import org.hibernate.Session;
4
5import junit.framework.TestCase;
6
7/** *//**
8 * 所有lazy属性默认
9 * @author Administrator
10 *
11 */
12public class SingleEndTest1 extends TestCase {
13
14 public void testLoad1() {
15 Session session = null;
16 try {
17 session = HibernateUtils.getSession();
18 session.beginTransaction();
19
20 //不会发出sql
21 User user = (User)session.load(User.class, 1);
22
23 //会发出sql
24 System.out.println("user.name=" + user.getName());
25
26 //不会发出sql
27 Group group = user.getGroup();
28
29 //会发出sql
30 System.out.println("group.name=" + group.getName());
31
32 session.getTransaction().commit();
33 }catch(Exception e) {
34 e.printStackTrace();
35 session.getTransaction().rollback();
36 }finally {
37 HibernateUtils.closeSession(session);
38 }
39 }
40
41}
42
SingleEndTest2.java
1package com.bjsxt.hibernate;
2
3import org.hibernate.Session;
4
5import junit.framework.TestCase;
6
7/** *//**
8 * 将<many-to-one>中的lazy设置为false,其它默认
9 * @author Administrator
10 *
11 */
12public class SingleEndTest2 extends TestCase {
13
14 public void testLoad1() {
15 Session session = null;
16 try {
17 session = HibernateUtils.getSession();
18 session.beginTransaction();
19
20 //不会发出sql
21 User user = (User)session.load(User.class, 1);
22
23 //会发出sql,发出两条sql分别加载User和Group
24 System.out.println("user.name=" + user.getName());
25
26 //不会发出sql
27 Group group = user.getGroup();
28
29 //不会发出sql
30 System.out.println("group.name=" + group.getName());
31
32 session.getTransaction().commit();
33 }catch(Exception e) {
34 e.printStackTrace();
35 session.getTransaction().rollback();
36 }finally {
37 HibernateUtils.closeSession(session);
38 }
39 }
40
41}
42
1package com.bjsxt.hibernate;
2
3import org.hibernate.Session;
4
5import junit.framework.TestCase;
6
7/** *//**
8 * 将<many-to-one>中的lazy设置为false,其它默认
9 * @author Administrator
10 *
11 */
12public class SingleEndTest2 extends TestCase {
13
14 public void testLoad1() {
15 Session session = null;
16 try {
17 session = HibernateUtils.getSession();
18 session.beginTransaction();
19
20 //不会发出sql
21 User user = (User)session.load(User.class, 1);
22
23 //会发出sql,发出两条sql分别加载User和Group
24 System.out.println("user.name=" + user.getName());
25
26 //不会发出sql
27 Group group = user.getGroup();
28
29 //不会发出sql
30 System.out.println("group.name=" + group.getName());
31
32 session.getTransaction().commit();
33 }catch(Exception e) {
34 e.printStackTrace();
35 session.getTransaction().rollback();
36 }finally {
37 HibernateUtils.closeSession(session);
38 }
39 }
40
41}
42
SingleEndTest3.java
1package com.bjsxt.hibernate;
2
3import org.hibernate.Session;
4
5import junit.framework.TestCase;
6
7/** *//**
8 * <class>标签上的lazy=false,其它默认
9 * @author Administrator
10 *
11 */
12public class SingleEndTest3 extends TestCase {
13
14 public void testLoad1() {
15 Session session = null;
16 try {
17 session = HibernateUtils.getSession();
18 session.beginTransaction();
19
20 //会发出sql
21 User user = (User)session.load(User.class, 1);
22
23 //不会发出sql
24 System.out.println("user.name=" + user.getName());
25
26 //不会发出sql
27 Group group = user.getGroup();
28
29 //会发出sql
30 System.out.println("group.name=" + group.getName());
31
32 session.getTransaction().commit();
33 }catch(Exception e) {
34 e.printStackTrace();
35 session.getTransaction().rollback();
36 }finally {
37 HibernateUtils.closeSession(session);
38 }
39 }
40
41}
42
1package com.bjsxt.hibernate;
2
3import org.hibernate.Session;
4
5import junit.framework.TestCase;
6
7/** *//**
8 * <class>标签上的lazy=false,其它默认
9 * @author Administrator
10 *
11 */
12public class SingleEndTest3 extends TestCase {
13
14 public void testLoad1() {
15 Session session = null;
16 try {
17 session = HibernateUtils.getSession();
18 session.beginTransaction();
19
20 //会发出sql
21 User user = (User)session.load(User.class, 1);
22
23 //不会发出sql
24 System.out.println("user.name=" + user.getName());
25
26 //不会发出sql
27 Group group = user.getGroup();
28
29 //会发出sql
30 System.out.println("group.name=" + group.getName());
31
32 session.getTransaction().commit();
33 }catch(Exception e) {
34 e.printStackTrace();
35 session.getTransaction().rollback();
36 }finally {
37 HibernateUtils.closeSession(session);
38 }
39 }
40
41}
42