当前位置: 首页 > 工具软件 > Session Buddy > 使用案例 >

org.hibernate.LazyInitializationException: could not initialize proxy xxx - no Session 错误解决

狄楷
2023-12-01

问题

使用hibernate的getReference()方法时返回could not initialize proxy的错误。

发生场景
在Spring Boot项目中测试JPA的getReference() 方法,JPA的实现是Hibernate。

    @Test

    public void getReference() {
       String id= "1";
       Usr usr =  em.getReference(Usr.class, id);
       System.out.println(usr.getName());
    }

执行后报错误信息如下:



org.hibernate.LazyInitializationException: could not initialize proxy [xx] - no Session
	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169)
	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)
	at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
	at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)

原因及解析

 类似资料: