gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:58:19 24 4
gpt4 key购买 nike

我收到错误: 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com