spring boot + spring-data-jpa项目 在启动初始化数据时出现懒加载经典错误,从浏览器请求数据时,正常

zeroxiao 发布于 2017/12/29 12:37
阅读 1K+
收藏 2

解读下一代网络:算力网络正从理想照进现实!>>>

spring boot + spring-data-jpa项目

在启动初始化一些数据时出现懒加载经典错误

从浏览器请求数据时,正常

已设置

spring:
 jpa:
  open-in-view: true

已配置

@Bean
public FilterRegistrationBean registerOpenSessionInViewFilterBean() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(new OpenEntityManagerInViewFilter());
    filterRegistrationBean.setOrder(-10);
    filterRegistrationBean.setEnabled(true);
    filterRegistrationBean.addUrlPatterns("/*");
    Map<String, String> initParameters = Maps.newHashMap();
    initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*,/webjars/*,/druid/*,/static/*");
    filterRegistrationBean.setInitParameters(initParameters);
    return filterRegistrationBean;
}

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: win.qingting.security.domain.SysMenu.roles, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at win.qingting.security.shiro.filter.ShiroFilterChainDefinitionMenus.reloadPermission(ShiroFilterChainDefinitionMenus.java:73) ~[classes/:na]
    at win.qingting.security.listener.ApplicationSecurityStartup.run(ApplicationSecurityStartup.java:55) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    ... 9 common frames omitted
 

加载中
0
RNT117
RNT117

在出问题的实体类上加@Proxy(lazy = false)

@Proxy(lazy = false)
public class MyClass{

}

 

返回顶部
顶部