org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jav

错误原因:

原因是在hibernate映射关系中由于延迟加载,session在调用前已经被关闭,所以加载set属性时无可用session

解决方案1(使用的是注解配置):

在 @ManyToOne端设置fetch=FetchType.LAZY,@OneToMany端设置fetch=FetchType.EAGER,如下配置

@ManyToOne(cascade={CascadeType.REFRESH, CascadeType.MERGE}, optional=true, fetch=FetchType.LAZY)
@JoinColumn(name="uim_serv_file_record_id")
private UimServFileRecord uimServFileRecord;

                              

@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="uimServFileRecord")
private Set uimServFileDatas=new HashSet();

解决方案2(使用的是xml配置):

1,把lazy=”false“

2,在web.xml中加入:(在structs的过滤器之前)

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jav
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 加入上面这些 -->
<filter> 
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>  
  <filter-mapping>  
      <filter-name>struts2</filter-name>  
      <url-pattern>/*</url-pattern>  
  </filter-mapping> 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jav

第1种方法,就是设置为不要用lazy加载元素,这样做,虽然可以解决上面这个问题,但是会产生一些不必要的开销。比较复杂的对象对应关系,或者数据量比较     大的时候,不推荐这么做
第2种方法,就是避免session关闭过早导致出现这样的异常,这样的方法以前也是使用过,而且一般情况下filter过滤链接都会做




相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2021-07-24
  • 2021-07-05
  • 2021-07-08
猜你喜欢
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
相关资源
相似解决方案