3 つの Bean があります: Organization、Role、User
役割 - 組織関係 - @ManyToOne
役割 - ユーザー関係 - @ManyToMany
組織 :
@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;
...
役割 :
@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;
...
ユーザー :
@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;
...
したがって、指定したユーザーのすべての組織を取得する必要があります (最初にすべてのユーザー ロールを選択し、このロールを持つすべての組織を選択する必要があります)。
このロジックを実現するSQLステートメントがあります(たとえば、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
休止状態の名前付きクエリメカニズムを使用して、これをどのように実装できますか? ありがとう!