如题,有Husband和Wife的两个类,他们是一对一的:
在Husband中,关于wife的注解 @OneToOne(mappedBy="husband",cascade={CascadeType.ALL},fetch=FetchType.LAZY)
public Wife getWife() {
return wife;
}在Wife中,关于husband的注解@JoinColumn(name="husband_hid")
@OneToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
public Husband getHusband() {
return husband;
}我在Husband中的注解是lazy,所以我get(Husband.class,id)的时候,按理说不应该加载出Wife呀,可是它却打印出来了!
在控制台下显示的sql语句也打印了:Hibernate:
select
husband0_.hid as hid0_0_,
husband0_.name as name0_0_
from
zf_husband husband0_
where
husband0_.hid=?
Hibernate:
select
wife0_.wid as wid1_1_,
wife0_.husband_hid as husband3_1_1_,
wife0_.name as name1_1_,
husband1_.hid as hid0_0_,
husband1_.name as name0_0_
from
zf_wife wife0_
left outer join
zf_husband husband1_
on wife0_.husband_hid=husband1_.hid
where
wife0_.husband_hid=?
这是怎么回事呀,设置lazy不是不查询了吗?
我测试的方法:@Test
public void testSelectHusbandWithWife() {
Session session = cfg.buildSessionFactory().getCurrentSession();
session.beginTransaction();
Husband husband = (Husband)session.get(Husband.class,3);
session.getTransaction().commit();
System.out.println(husband.toString());
System.out.println(husband.getWife().getName());
}我重写了toString()方法。
在Husband中,关于wife的注解 @OneToOne(mappedBy="husband",cascade={CascadeType.ALL},fetch=FetchType.LAZY)
public Wife getWife() {
return wife;
}在Wife中,关于husband的注解@JoinColumn(name="husband_hid")
@OneToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
public Husband getHusband() {
return husband;
}我在Husband中的注解是lazy,所以我get(Husband.class,id)的时候,按理说不应该加载出Wife呀,可是它却打印出来了!
在控制台下显示的sql语句也打印了:Hibernate:
select
husband0_.hid as hid0_0_,
husband0_.name as name0_0_
from
zf_husband husband0_
where
husband0_.hid=?
Hibernate:
select
wife0_.wid as wid1_1_,
wife0_.husband_hid as husband3_1_1_,
wife0_.name as name1_1_,
husband1_.hid as hid0_0_,
husband1_.name as name0_0_
from
zf_wife wife0_
left outer join
zf_husband husband1_
on wife0_.husband_hid=husband1_.hid
where
wife0_.husband_hid=?
这是怎么回事呀,设置lazy不是不查询了吗?
我测试的方法:@Test
public void testSelectHusbandWithWife() {
Session session = cfg.buildSessionFactory().getCurrentSession();
session.beginTransaction();
Husband husband = (Husband)session.get(Husband.class,3);
session.getTransaction().commit();
System.out.println(husband.toString());
System.out.println(husband.getWife().getName());
}我重写了toString()方法。
解决方案 »
- 一个关于mybatis动态拼接SQL的问题
- SQL 语句问题求帮助
- 如何查询JS中是否存在指定的字符,在线等
- Oracle字段中有空格时,hibernate怎么映射???
- 关于jboss和Myeclipse8.5的EJB访问方式
- 怎么将从数据库取到的值放到combotree的文本框里
- javabeans的问题
- struts1.0的问题
- 200分请高手指点: jsp怎样调用webservice 解决再送100分,在线等,立即给分
- Spring boot导出数据Excel,整体层次有哪些类
- Exception sending context initialized event to listener instance of class
- Hibernate 怎么直接获取oracle的系统时间?
这不已经getWife了么