failed to lazily initialize a collection of

hibernate多对多关系,获取数据的时候报“failed to lazily initialize a collection of”,在页面的显示“ognl.NoConversionPossible”。

在网上查了一下资料,原来是懒加载配置的问题,修改懒加载配置即可结局,原理暂时还不太了解。

xml配置方式
设置 lazy=”false”
例:

     <many-to-one name="childQuestion" class="com.newland.netsurvey.model.Question" lazy="false" fetch="select">
        <column name="childQuestionId" />
     </many-to-one>

@注解方式
在NtoN的括号里添加 fetch=FetchType.EAGER
例子:

//多对多
@ManyToMany(fetch=FetchType.EAGER)
//一对多
@OneToMany(  
   targetEntity = CourseAuthorizationItem.class,  
   cascade = {CascadeType.PERSIST, CascadeType.MERGE},  
   mappedBy = "course", fetch=FetchType.EAGER  
  )  

猜你喜欢

转载自blog.csdn.net/a241903820/article/details/51935028
今日推荐