UserDetail.java(模型类)
@Entity
public class UserDetail {
@Id
private String email;
private String name;
private String password;
@ManyToMany(cascade = CascadeType.ALL)
private List<Role> role;
@ManyToMany(cascade = CascadeType.ALL,mappedBy = "user")
private List<GroupDetail> group;
}
GroupDetail.java(模型类)
@Entity
public class GroupDetail {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(unique = true)
private String groupName;
@ManyToMany(cascade = CascadeType.ALL)
private List<UserDetail> user;
@ManyToMany(cascade = CascadeType.ALL)
private List<Role> role;
}
在这里您可以看到GroupDetail是关联的所有者,并且Hibernate仅在维护关联时检查该侧。
那么,如何使用用户电子邮件获取GroupDetail列表?
答案 0 :(得分:0)
以下应该对您有用:
select userDtl.group from UserDetail userDtl where userDtl.email = 'Your Email'
如果这不起作用,那么您可以尝试像这样显式使用join:
select g from UserDetail u join u.group g where u.email = 'Your Email'
答案 1 :(得分:0)
执行此操作,在两个实体中添加getter和setter。
UserDetails user = em.find(UserDetails.class, emailId);
List<GroupDetail> group = user.getGroup();