我有3个实体-用户组,用户,用户组成员身份。 usergroupmemberships对用户和用户组都是1到1。我正在尝试获取具有其ID的用户组
以下查询可在我的数据库中使用:
SELECT *
FROM UserGroup g
JOIN UserGroupMembership ON
UserGroupMembership.usergroup_id = g.id
WHERE UserGroupMembership.user_id = 868
Wildfly:
与使用来自我的entitymanager的查询的Java REST POST相同,没有nullpointerexception
SELECT g
FROM UserGroup g
JOIN UserGroupMembership
ON UserGroupMembership.usergroup = g.id
WHERE UserGroupMembership.user = :id
我该如何正确书写?
尝试了不同的方式来编写查询,并尝试编写user_id 即时通讯在wildfly和Java ee上
public List<UserGroup> findUserGroupbyUser(long id) {
return em.createQuery("SELECT g FROM UserGroup g JOIN
UserGroupMembership ON UserGroupMembership.usergroup = g WHERE
UserGroupMembership.user.id = :id", UserGroup.class).setParameter("id",
id).getResultList();
}
@Entity
public class UserGroupMembership {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(cascade = CascadeType.ALL)
private UserGroup usergroup;
@OneToOne(cascade = CascadeType.ALL)
private User user;
预期结果是1个条目