java - 延迟初始化异常 : could not initialize proxy - no Session

标签 java spring-boot spring-data spring-data-jpa

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

源代码

用户.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
} 

用户存储库.java
public interface UserRepository extends JpaRepository<User, Integer> {

}

用户服务测试.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

异常详情
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.

最佳答案

您可以添加 @Transactional注释到您的测试方法以避免此异常。

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

Get an instance, whose state may be lazily fetched.



在 Spring 中实现 EntityManagerorg.hibernate.internal.SessionImpl - 所以如果没有 Session Spring 就无法获得这个方法。

要进行 session ,您只需创建一个事务...

关于java - 延迟初始化异常 : could not initialize proxy - no Session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49894587/

相关文章:

java - 如何在Spring Data中使用query模仿sql查询

java - 如何在 Hibernate 中获取 <map>

java - LocalDateTime Java - 获取下个月的同一天

java - spring mongodb - 将管道聚合到java中

java - Spring 5 处理 null Beans 的方式发生了变化?

java - 配置Spring Data错误消息

spring - 是否有任何 Spring Annotation 为字段(Mongo)设置默认值?

java - [Gradle][Play]compilePlayBinaryScala 定义在哪里?

java.lang.ClassCastException : java. io.ObjectStreamClass 无法转换为 [MyClass]

java - 更改Tomcat6服务器中的java堆大小