I have a User
entity which can be a Manager
or Client
, Manager
can have many Clients
and Client
can have many Managers
.
I tried to map the User
entity like this:
@Id
@GeneratedValue(strategy = AUTO)
private UUID uuid;
@ManyToMany
@JoinTable(name = "managers_clients",
joinColumns = {
@JoinColumn(name = "clientUuid", referencedColumnName = "uuid", nullable = false)},
inverseJoinColumns = {
@JoinColumn(name = "managerUuid", referencedColumnName = "uuid", nullable = false)})
private List<UserEntity> managers;
@ManyToMany(mappedBy = "managers")
private List<UserEntity> clients;
But unfortunately, I have an error:
failed to lazily initialize a collection of role: com.company.domain.common.entities.UserEntity.managers, could not initialize proxy - no Session
Can someone explain why this happening and how can I overcome this issue? Many thanks!
The problem is that you do not have a session during the fetching of the data. I'm not sure where you are fetching data, but try to add @Transactional to this method.
This will make sure the JPA uses a session to get the data.
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.