今天程序报了这样一个错:could not initialize proxy - no Session. 原因是在web.xml 中新增了
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/test/*</url-pattern>
</filter-mapping>
而在 web.xml 中的 是这样配置的, 而我在 struts2 的action 中调用了一个对象的关联对象属性, 导致了报这个错误
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
原理:
因为我们在hibernate里面load一个对象出来时,用到的是代理对象,也就是说当我们在执行load方法时并没有发sql语句,而是返回一个proxy对象。只有当们具体用到哪个get**方法时才会发sql语句,才会去数据库查。但是当我们把打开session,关闭session交给了srping去做时,当们load完之后我们的session就会被srping关闭,如果我们在jsp页面或者其它的地方再去用get方法取值时就会报这个错误。
解决方法:
方法一:用到srping的filter(要加在strutsfilter有前面,因为它也有先后顺序,有先进先出的原则)
在我们的web.xml里面加上
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样做就是让opensession closesession全交给视图部分,最后视图部分用完了session再去关session就不会有上面的错误了
方法二:
直接不用懒加载,@ManyToOne(fetch=FetchType.LAZY) 改为@ManyToOne(fetch=FetchType.EAGER)
但是有时候会报错, 就是没有对应的列, 但是数据库中明明是有的, 只能用一个笨方法, 就是在 service 中调用一下关联对象的属性
例如: A 中有一个关联对象 B, 那么在 sesrvice 中查询出 A 之后, 调用一下 a.getB().getName() 方法, 这样 B 就被加载出来了(name 为 B 的一个属性), 这样在 action 中再调用 a.getB()
方法就不会报错了
分享到:
相关推荐
2.2.3.1+spring-framework-3.1.0+hibernate-distribution-3.6.8+JSON+MySQL+Annotation,并且对Dao和Service进行了封装,内含.jar包,并且解决了一对多双向关联的could not initialize proxy - no Session错误,同时...
解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session...
7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 ...
NULL 博文链接:https://smartgwt.iteye.com/blog/1252268
主要介绍了Plsql Developer连接Oracle时出现Could not initialize oci.dll解决方案,需要的朋友可以参考下
7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 ...
解决:oracle_plsql连服务端时 Initialization error could initialize错误处理方法
监听COM口启动程序报错 Could not initialize class gnu.io.RXTXCommDriver异常 是因为需要在%JAVA_HOME%/jre/bin目录中添加rxtxParallel.dll、rxtxSerial.dll文件
内涵jacob_1.14.3-x64.dll;jacob_1.14.3-64.jdk(maven地址);解决方法:Could not initialize class com.jacob.com.ComThread
使用javacpp技术,在32位linux系统上实现视频截图功能,注意so文件放在liberary_path里面
java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.AutoloadedDriver40 导致的原因: 在azkaban的server和executor中缺少一个叫derby.jar的包
Could not initialize "D:\app\Happy\product\11.2.0\dbhome_1\bin\oci.dll" Make sure you have the 32 bits Oracle Client installed. OracleHomeKey: OracleHomeDir: D:\app\Happy\product\11.2.0\dbhome_1 ...
instantclient-basic-windows.x64-11.2.0.4.0
IDA6.4中支持pin的动态二进制插桩功能,这是其中的描述文档
解压缩版tomcat7,可直接配置web项目,结合eclipse或Intellij IDEA均可。
Win7 x64 PL/SQL 连接 Oralce 提示 Could not initialize "%ORACLE_HOME%\bin\oci.dll" 缺少oci文件
unable to initialize windows sockets interface error code 0 WINSOCK坏了
JSONArray.fromObject(map)报错:Could not initialize class net.sf.json.JsonConfig。ireport 需要高于2.1版本的包。于是就找了这些包。最后2.2.2适合
// Initialize the proxy server // Also specify any options for hyperswarm here // https://github.com/hyperswarm/hyperswarm const server = new HyperswarmServer ( ) // Start listening on clients via ...