I'm getting the error: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.fleetx.persistence.model.Account.users, could not initialize proxy - no Session
when I try to do a create operation on one of the entities I'm trying to audit using Javers. The following is the structure of my application:
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
}
Account.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
}
I don't want to change the fetch type of private Account account;
or private List<User> users;
to FetchType.EAGER
.
Any help would be appreciated. Thankyou.
If you don't want to change to FetchType.EAGER
then wherever you are accessing Account.getUsers()
needs to be in a transaction so the Hibernate Session isn't discarded. The easiest way to do this is to add the @Transactional
to the method of the service or bean where the fetching and getUsers() access is being done. Make sure Transactions are turned on with @EnableTransactionManagement
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.