hibernate在单端关联上的lazy策略,可以取值:false/proxy/noproxy,默认lazy=proxy
<class>标签上的lazy不会影响到单端关联上的lazy特性
1.实体类
2.实体类映射文件
三、测试类1:SingleEndTest1.java
测试类2:SingleEndTest2.java
测试类3:SingleEndTest3.java
<class>标签上的lazy不会影响到单端关联上的lazy特性
1.实体类
package com.yx.zzg.bo;
public class Group {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.yx.zzg.bo;
public class User {
private int id;
private String name;
private Group group;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
2.实体类映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yx.zzg.bo.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="group" column="groupid"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yx.zzg.bo.Group" table="t_group">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
三、测试类1:SingleEndTest1.java
package com.yx.zzg.test;
import org.hibernate.Session;
import junit.framework.TestCase;
import com.yx.zzg.bo.*;
/**
* 所有lazy属性默认
* @author Administrator
*
*/
public class SingleEndTest1 extends TestCase {
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//不会发出sql
User user = (User)session.load(User.class, 1);
//会发出sql
System.out.println("user.name=" + user.getName());
//不会发出sql
Group group = user.getGroup();
//会发出sql
System.out.println("group.name=" + group.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
测试类2:SingleEndTest2.java
package com.yx.zzg.test;
import org.hibernate.Session;
import junit.framework.TestCase;
import com.yx.zzg.bo.*;
/**
* 将<many-to-one>中的lazy设置为false,其它默认
* @author Administrator
*
*/
public class SingleEndTest2 extends TestCase {
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//不会发出sql
User user = (User)session.load(User.class, 1);
//会发出sql,发出两条sql分别加载User和Group
System.out.println("user.name=" + user.getName());
//不会发出sql
Group group = user.getGroup();
//不会发出sql
System.out.println("group.name=" + group.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
测试类3:SingleEndTest3.java
package com.yx.zzg.test;
import org.hibernate.Session;
import junit.framework.TestCase;
import com.yx.zzg.bo.*
/**
* <class>标签上的lazy=false,其它默认
* @author Administrator
*
*/
public class SingleEndTest3 extends TestCase {
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//会发出sql
User user = (User)session.load(User.class, 1);
//不会发出sql
System.out.println("user.name=" + user.getName());
//不会发出sql
Group group = user.getGroup();
//会发出sql
System.out.println("group.name=" + group.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}