LazyInitializationException:无法初始化代理 - 没有会话 [英] LazyInitializationException: could not initialize proxy - no Session

查看:26
本文介绍了LazyInitializationException:无法初始化代理 - 没有会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用spring-data-jpaspring-boot(v2.0.0.RELEASE),我刚在MySQL上写了一个CRUD demo,但是在执行过程中出现异常运行时,源码如下:

源代码

User.java

@Entity公共类用户实现可序列化{private static final long serialVersionUID = 1L;@ID私有整数 ID;私人字符串用户名;私人字符串密码;...getter&setter}

UserRepository.java

public interface UserRepository extends JpaRepository{}

UserServiceTest.java

@RunWith(SpringRunner.class)@SpringBootTest公共类 UserServiceTest {@自动连线私有 UserRepository userRepository;@测试public void getUserById() 抛出异常{userRepository.getOne(1);}}

application.yml

弹簧:数据源:用户名: ***密码: ***驱动程序类名:com.mysql.jdbc.Driver网址:********百里香:缓存:假日文:显示 sql: 真休眠:ddl-auto:更新

异常详情

org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话在 org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:155)在 org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:268)在 org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)在 cn.shuaijunlan.sdnsecuritysystem.domain.po.User_$$_jvstc90_0.getUsername(User_$$_jvstc90_0.java)在 cn.shuaijunlan.sdnsecuritysystem.service.UserServiceTest.getUserById(UserServiceTest.java:33)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:498)在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

<块引用>

我尝试了另一种方法userRepository.findOne(1),它可以成功运行.

解决方案

您可以在测试方法中添加 @Transactional 注释以避免此异常.

方法 getOne 返回可以延迟加载属性的实体的引用"(代理).看到它 code - 它使用 EntityManagergetReference 方法.从它javadoc:

<块引用>

获取一个实例,其状态可能会被延迟获取.

在 Spring 中,EntityManager 的实现是 org.hibernate.internal.SessionImpl - 所以没有 Session Spring 不能得到这个方法.

要进行会话,您只需创建一个交易...

I use spring-data-jpa with spring-boot(v2.0.0.RELEASE), I just wrote a CRUD demo on MySQL, but an exception occurs during runtime, source code as follows:

Source code

User.java

@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;
    private String username;
    private String password;

    ...getter&setter
} 

UserRepository.java

public interface UserRepository extends JpaRepository<User, Integer> {

}

UserServiceTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void getUserById() throws Exception{
        userRepository.getOne(1);
    }

}

application.yml

spring:
  datasource:
    username: ***
    password: ***
    driver-class-name: com.mysql.jdbc.Driver
    url: ********
  thymeleaf:
    cache: false
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update

Exception details

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:155)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:268)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)
at cn.shuaijunlan.sdnsecuritysystem.domain.po.User_$$_jvstc90_0.getUsername(User_$$_jvstc90_0.java)
at cn.shuaijunlan.sdnsecuritysystem.service.UserServiceTest.getUserById(UserServiceTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

I try to another method userRepository.findOne(1), it can run successfully.

解决方案

You can add @Transactional annotation to your test method to avoid this exception.

Method getOne return the 'reference' (proxy) of the entity which properties can be lazy loaded. See it code - it uses getReference method of EntityManager. From it javadoc:

Get an instance, whose state may be lazily fetched.

In Spring the implementation of EntityManager is org.hibernate.internal.SessionImpl - so without the Session the Spring can not get this method.

To have a session you can just create a transaction...

这篇关于LazyInitializationException:无法初始化代理 - 没有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆