我有一个Post类,如下所示,它与用户(可以看到这篇文章的人)有一对多的关系,并且它与组具有相同的关系(一对多) ,现在我想通过基于用户和组的JPA查询获取所有帖子,我该如何为此编写JPA查询?
/* Post */
public class Post extends Model {
private String title;
private String text;
@OneToMany
@JoinTable(
name="tbl_post_users",
inverseJoinColumns={@JoinColumn(name="userId")},
joinColumns={@JoinColumn(name="postId")}
)
public List<User> users;
@OneToMany
@JoinTable(
name="tbl_post_groups",
inverseJoinColumns={@JoinColumn(name="groupId")},
joinColumns={@JoinColumn(name="postId")}
)
public List<Group> groups;
public static List<Post> GetPosts(){
// JPA Query here
}
}
/* User */
public class User extends Model {
@Id
private int id;
private String name;
private String email;
}
/* Group */
public class Group extends Model {
@Id
private int id;
private String title;
private int ownerId;
}
所以我想编写查询,以便我可以获取用户为{user}或group为{group}
的所有帖子的列表答案 0 :(得分:0)
select distinct p from Post p
left join p.users user
left join p.groups group
where user = :user
or group = :group
阅读http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-joins