hibernate是一个用于连接数据库的升级版JDBC,与一般JDBC不同的是,hibernate架构是通过hql进行查找,通过session建立连接。其工作原理是:将实例分为瞬时状态、持久状态、游离状态,通过hql对持久状态的实例进行CRUD。在学习的过程中我们遇到了这样的一个现象:用Session对象调用get()方法和load()方法获取对象时,类似的代码会分为两种结果,get方法运行成功,load方法会报错,代码示例如下:
get()方法源代码:
Session session= HibernateSessionFactory.getSession();
SysUserEntity user=(SysUserEntity)session.get(SysUserEntity.class,id);
HibernateSessionFactory.closeSession();
运行结果:代码运行成功
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select sysuserent0_.userId as userId1_0_0_, sysuserent0_.user as
user2_0_0_, sysuserent0_.loginPassWord as loginPas3_0_0_, sysuserent0_.loginUser as
loginUse4_0_0_ from sys_user sysuserent0_ where sysuserent0_.userId=?
曹操
load()方法源代码:
Session session= HibernateSessionFactory.getSession();
SysUserEntity user=(SysUserEntity)session.load(SysUserEntity.class,id);
HibernateSessionFactory.closeSession();
运行结果:代码报错原因是在关闭了session
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at com.lovo.h.entity.SysUserEntity_$$_jvstb09_0.getLoginUser(SysUserEntity_$$_jvstb09_0.java)
at com.lovo.h.test.T.main(T.java:36)
session中的load()方法默认使用了懒加载,也就是说调用load()会把数据加载到缓存中,而只有当实际调用时才会在数据库中执行查找语句,这样就引起了矛盾:load()得到的实例是持久状态的实例,实际调用时已经关闭了session,那么此时的实例就是游离状态的实例,只有持久状态的实例才能在数据库中得到数据,因此会抛出异常。