看下面的例子
user和班级,多对一得关系。
目标:希望查询学生的时候,知道学生所在班级信息
public class User { private Long userId; private String userName; private GradeClass gradeClass; ...//省略get和set方法
User.hbm.xml
<hibernate-mapping> <class name="com.supben.pojo.User" table="User"> <id name="userId" type="java.lang.Long"> <column length="20" name="userId" /> <generator class="identity"></generator> </id> <property name="userName" type="java.lang.String"> <column name="userName" /> </property> <!--column="gradeClassId" 表示的是user表中和班级表关联字段gradeClassId --> <many-to-one name="gradeClass" column="gradeClassId" class="com.supben.pojo.GradeClass" /> </class> </hibernate-mapping>
GradeClass.hbm.xml,和平时的配置没有任何的不同!
<hibernate-mapping> <class name="com.supben.pojo.GradeClass" table="gradeclass"> <id name="gradeClassId" type="java.lang.Long"> <column length="20" name="gradeClassId" /> <generator class="identity"></generator> </id> <property name="gradeClassName" type="java.lang.String"> <column name="gradeClassName" /> </property> </class>
测试代码
public void testFind() { List<User> userList = userService.findAll(); assertEquals(2, userList.size()); System.out.println("*************************************"); System.out.println(userList.get(0).getGradeClass().getGradeClassName()); ...//(省略dao和service中的查询方法) }
Hibernate: select this_.userId as userId1_0_, this_.userName as userName1_0_, this_.gradeClassId as gradeCla3_1_0_ from User this_
*************************************
1084 [main] ERROR org.hibernate.LazyInitializationException - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.supben.pojo.GradeClass_$$_javassist_1.getGradeClassName(GradeClass_$$_javassist_1.java)
at com.supben.service.UserServiceTest.testFind(UserServiceTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
可以看到hibernate 3 many-to-one 默认是延迟加载的,出现了异常:不能初始化代理对象,session超时。
把配置文件改成lazy="false"呢,即这样配置
Xml代码
<many-to-one name="gradeClass" column="gradeClassId" class="com.supben.pojo.GradeClass" lazy="false" />
运行结果一切正常,在拿到user对象之后,立即发出2条sql去拿到班级对象。
Hibernate: select this_.userId as userId1_0_, this_.userName as userName1_0_, this_.gradeClassId as gradeCla3_1_0_ from User this_
Hibernate: select gradeclass0_.gradeClassId as gradeCla1_0_0_, gradeclass0_.gradeClassName as gradeCla2_0_0_ from gradeclass gradeclass0_ where gradeclass0_.gradeClassId=?
Hibernate: select gradeclass0_.gradeClassId as gradeCla1_0_0_, gradeclass0_.gradeClassName as gradeCla2_0_0_ from gradeclass gradeclass0_ where gradeclass0_.gradeClassId=?
*************************************
101
优化:在配置文件里加上fetch="join"在运行,可以看到控制台的输出为:
Hibernate: select this_.userId as userId1_1_, this_.userName as userName1_1_, this_.gradeClassId as gradeCla3_1_1_, gradeclass2_.gradeClassId as gradeCla1_0_0_, gradeclass2_.gradeClassName as gradeCla2_0_0_ from User this_ left outer join gradeclass gradeclass2_ on this_.gradeClassId=gradeclass2_.gradeClassId
*************************************
101
只发出了一条sql就获得了所有的结果。这是因为hibernate默认是用fetch="select"的方式,即根据gradeClassId去select。改成了join,就会一次性查出