问题:
使用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)
原因及解析