首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >LazyInitializationException:无法初始化代理-没有会话

LazyInitializationException:无法初始化代理-没有会话
EN

Stack Overflow用户
提问于 2018-04-18 16:17:00
回答 4查看 27.9K关注 0票数 16

我使用spring-data-jpaspring-boot(v2.0.0.RELEASE),我刚刚在MySQL上写了一个CRUD演示,但是在运行时发生了一个异常,源代码如下:

源代码

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

异常详细信息

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)

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-04-18 20:55:21

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

方法getOne返回可以延迟加载属性的实体的“引用”(代理)。查看code -它使用EntityManagergetReference方法。来自它的javadoc:

获取一个实例,该实例的状态可能是延迟获取的。

在Spring中,EntityManager的实现是org.hibernate.internal.SessionImpl --所以如果没有会话,Spring就不能获得这个方法。

要创建会话,您只需创建一个事务...

票数 23
EN

Stack Overflow用户

发布于 2019-08-15 03:50:21

你的测试应该是这样的:

@RunWith(SpringRunner.class)    


@SpringBootTest
@Transactional    

public class QuestionTesting {   

    @Test    
    public void test() {    

    }    
}    
票数 3
EN

Stack Overflow用户

发布于 2018-04-18 17:17:40

在服务类中,请使用注释@PersistenceContext为实体管理器添加setter。具体地说,使用

 @PersistenceContext(type = PersistenceContextType.EXTENDED)

通过这种方式,您可以访问惰性属性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49894587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档