java - org.hibernate.LazyInitializationException : failed to lazily initialize a collection of role while using Javers

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

我收到错误: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.fleetx.persistence.model.Account.users, could not initialize proxy - no Session

当我尝试对我尝试使用 Javers 进行审核的实体之一执行创建操作时。以下是我的应用程序的结构:

AudityEntity.java

@Data
@Entity
@Table
public class AuditEntity extends Base {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_id", updatable = false)
    @JsonIgnore
    private Account account;

    // other fields
}

帐户.java

@Data
@Entity
@Table
public class Account extends Base {
    @OneToMany(mappedBy = "account", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private List<User> users;

    // other fields
}

AuditEntityRepository.java

@JaversSpringDataAuditable
public interface AuditEntityRepository extends JpaRepository<AuditEntityRepository, Long> {
    // crud operations
}

我不想更改 private Account account; 的获取类型或private List<User> users;FetchType.EAGER .

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

如果您不想更改为 FetchType.EAGER,那么无论您在何处访问 Account.getUsers() 都需要处于事务中,以便 Hibernate session 不存在没有被丢弃。最简单的方法是将 @Transactional 添加到正在执行获取和 getUsers() 访问的服务或 bean 的方法中。确保使用 @EnableTransactionManagement 打开事务。

关于java - org.hibernate.LazyInitializationException : failed to lazily initialize a collection of role while using Javers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430883/

相关文章:

java - 是否可以使用 JavaMail API 获取特定邮件的电子邮件服务提供商

java - Hibernate缓存技术

spring-boot - 使用 Kotlin 观察参数类型不匹配的 Spring Amqp Remoting

spring-boot - Spring Boot Actuator PoolingHttpClientConnectionManager 指标

java - Cygwin 编码困难

java - 泛型类 - Java 中的初始化

Java-ee 上下文查找总是失败

java - org.hibernate.service.UnknownServiceException : Unknown service requested [org. hibernate.ogm.service.impl.OgmConfigurationService]

java - 现有代码抛出此错误 [Hibernate + c3p0 + Oracle11g] : a resourcepool could not acquire a resource from its primary factory or source

java - Spring Boot WIndows 10 系统属性 'file.encoding'