解决Hibernate:could not initialize proxy - no Session
来源:互联网 发布:软件测试基础知识总结 编辑:程序博客网 时间:2024/06/05 04:03
在Hibernate中,<many-to-one.../>中的lazy默认为proxy。这样hibernate在数据库中查询数据时事不会把关联的
对象查出来的,而是保存一个获得该值得方法:getXxxx()。当我们需要使用这个值的时候,也就是使用getXxx()方法来
调用的时候,Hibernate就会利用这个方法从数据库中获取相应的数据。但是很不幸,我们的session早就关闭了。这
是因为Hibernate的懒加载策略,在Hibernate中是使用sessionFactory来管理session,我们每进行一次数据库操作时
都会新建一个session对象,当我们操作完成后,hibernate就会在dao层立即关闭该session。这样做就可以严格控制
session,避免出现低级错误。
对于这种错误,一般都会有三个方法可以解决。
1、把lazy设成false。这个是最简单的办法,个人认为也是比较笨的方法。因为这是在用效率作为代价。
2、使用OpenSessionInViewFilter。这种方法是将session交给servlet filter来管理,每当一个请求来之后就会开
启一个session,只有当响应结束后才会关闭。如下:
- <filter-name>hibernateFilter</filter-name>
- <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>
- </filter
- <filter-mapping>
- <filter-name>hibernateFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
上面的配置文件时在web.xml中配置的。
3、将hibernate的抓起策略改为join。也就是是left join fetch或inner join fetch语法。就是在<many-to-one../>中配
置lazy="false" fetch="join"即可。如:
- <many-to-one name="worker" lazy="false" fetch="join" class="com.paixie.domain.Worker">
- <column name="positionId"></column>
- </many-to-one>
- 解决Hibernate:could not initialize proxy - no Session
- 解决Hibernate:could not initialize proxy - no Session
- 解决Hibernate:could not initialize proxy - no Session
- 解决Hibernate:could not initialize proxy - no Session
- 解决Hibernate:could not initialize proxy - no Session
- 解决Hibernate:could not initialize proxy - no Session
- 解决Hibernate:could not initialize proxy - no Session
- Hibernate: could not initialize proxy - no Session
- hibernate错误:could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- 搭建hibernate环境
- 重命名要素类
- Escalation of Commitment(承诺升级)
- 当一个线程进入对象的synchronized方法后,其它线程是否可进入此对象的其它方法?
- Myeclipse10 安装Aptana插件
- 解决Hibernate:could not initialize proxy - no Session
- order by null
- Listview最后一行被覆盖的bug
- ocx
- 图像处理基本概念、术语
- sharedPreferences数据存储
- ubuntu14.04中mysql常用命令
- C#中Invoke的用法(转)
- linux常用命令的英文单词缩写