I have a Post class which is like below and it has a one-to-many relation with Users (people who can see this post) and also it has the same kind of relation (one-to-many) with Groups, now I want to get all the posts by a JPA Query based on Users and Groups, how should I write the JPA Query for this?
/* 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;
}
So I want to write the query so that I can get a list of all posts where user is {user} or group is {group}