[英]Many-To-Many relationship with same Entity
I have a User
entity which can be a Manager
or Client
, Manager
can have many Clients
and Client
can have many Managers
.我有一个
User
实体,它可以是Manager
或Client
, Manager
可以有很多Clients
,而Client
可以有很多Managers
。
I tried to map the User
entity like this:我试过 map 这样的
User
实体:
@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
未能延迟初始化角色集合:com.company.domain.common.entities.UserEntity.managers,无法初始化代理 - 否 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.问题是您在获取数据期间没有 session。 I'm not sure where you are fetching data, but try to add @Transactional to this method.
我不确定你在哪里获取数据,但尝试将 @Transactional 添加到此方法。
This will make sure the JPA uses a session to get the data.这将确保 JPA 使用 session 来获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.