views:

49

answers:

1

I have a question with HQL query and hibernate.

I have a user class and a role class. A user can have many roles. So I have a ManyToMany relatation like this:

In user class:

@ManyToMany(fetch = FetchType.LAZY)
@oinTable(name = "PORTAIL_USERROLE", joinColumns = { @JoinColumn(name = "USERID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "ROLE", nullable = false, updatable = false) })
public Set<Portailrole> getPortailroles() {
    return this.portailroles;
}

In role class:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "PORTAIL_USERROLE", joinColumns = { @JoinColumn(name = "ROLE", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "USERID", nullable = false, updatable = false) })
public Set<Portailuser> getPortailusers() {
    return this.portailusers;
}

This mapping has created a 3rd table (PORTAIL_USERROLE) where relations are stocked. All work fine like this. When I have a user, I retrieve roles.

But, my question is: in a HQL query, how can I get all users which have a specific role ? Any class represents PORTAIL_USERROLE table so I don't know how to make my HQL query.

+1  A: 

This should do it:

from Portailuser u join u.portailroles r where r.name=:roleName
Maurice Perry
Thanks a lot. I didn't think that hibernate manages manyToMany like this.
Kiva
Thats just linq isnt it?
Gage