I have 3 beans: Organization, Role, User
Role - Organization relation - @ManyToOne
Role - User relation - @ManyToMany
Organization :
@Entity
@Table(name = "entity_organization")
public class Organization implements Serializable {
private static final long serialVersionUID = -646783073824774092L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
Long id;
String name;
@OneToMany(targetEntity = Role.class, mappedBy = "organization")
List<Role> roleList;
...
Role :
@Entity
@Table(name = "entity_role")
public class Role implements Serializable {
private static final long serialVersionUID = -8468851370626652688L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
Long id;
String name;
String description;
@ManyToOne
Organization organization;
...
User :
@Entity
@Table(name = "entity_user")
public class User implements Serializable {
private static final long serialVersionUID = -4353850485035153638L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
Long id;
@ManyToMany
@JoinTable(name = "entity_user_role",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
List<Role> roleList;
...
So I need to get all Organizations for specified User ( first I need to select all user roles and than select all organizations that have this roles)
I have an sql statement that realizes this logic ( for e.g. I choose user with id = 1):
SELECT * FROM entity_organization AS o
INNER JOIN entity_role r ON r.organization_id = o.id
INNER JOIN entity_user_role ur ON ur.role_id=r.id
WHERE ur.user_id = 1
How can I implement this, using hibernate named query mechanism? Thanks!