ManyToMany relations in HQL
I've got the following relations:
public class Group {
private Intege开发者_Python百科r id;
(...)
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="rb_user_group",
joinColumns={@JoinColumn(name="group_id")},
inverseJoinColumns={@JoinColumn(name="user_id")})
private List<User> users;
and :
public class User {
private int id;
(...)
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="rb_user_group",
joinColumns={@JoinColumn(name="user_id")},
inverseJoinColumns={@JoinColumn(name="group_id")})
private List<Group> groups;
}
What is the proper way to select all users that belong to some group where group's name is set? I tried
session.createQuery("SELECT g.user FROM Group g WHERE g.name=:name").setString("name", group.getName());
but it doesn't help.
Try:
session.createQuery("SELECT u FROM User u left join u.groups as g with g = :group").setObject("group", group);
精彩评论