hibernateという名前のクエリ - 3つのテーブルに参加します
-
28-10-2019 - |
質問
私は3つの豆を持っています:組織、役割、ユーザー
役割 - 組織関係 - @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
Hibernateという名前のクエリメカニズムを使用して、これを実装するにはどうすればよいですか?ありがとう!
解決
@namedquery
以下を作成しました @NamedQuery
に Organization
エンティティクラス。
@NamedQuery(name = "query", query = "SELECT DISTINCT o " +
"FROM Organization o, User u " +
"JOIN o.roles oRole " +
"JOIN u.roles uRole " +
"WHERE oRole.id = uRole.id AND u.id = :uId")
public class Organization { ...
(標準のJPAアノテーションを使用しましたが、プロバイダーは冬眠でした。)
テスト
これは私が実行したテストです。
EntityManager em = ...
TypedQuery<Organization> q = em.createNamedQuery("query", Organization.class);
q.setParameter("uId", 1); // try it with 1L if Hibernate barks about it
for (Organization o : q.getResultList())
System.out.println(o.name);
以下のテーブルとサンプルデータを使用すると、この出力
A
B
それがあなたのために働くかどうかを見てください。
テーブル
CREATE TABLE `organization` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`organization_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);
CREATE TABLE `user_has_role` (
`user_id` int(11) NOT NULL DEFAULT '0',
`role_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`,`role_id`)
);
ALTER TABLE `role` ADD CONSTRAINT `cst_organization_id`
FOREIGN KEY `fk_organiztaion_id` (`organization_id`)
REFERENCES `organization` (`id`);
(私は少し異なりました あなたの, 、しかし、それはあまり重要ではないはずです。)
サンプルデータ
`organization`
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
+----+------+
`role`
+----+------+-------------+-----------------+
| id | name | description | organization_id |
+----+------+-------------+-----------------+
| 1 | A | a | 1 |
| 2 | B | b | 1 |
| 3 | C | c | 2 |
+----+------+-------------+-----------------+
`user`
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
`user_has_role`
+---------+---------+
| user_id | role_id |
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 3 | 1 |
| 3 | 3 |
+---------+---------+
他のヒント
以下のようにHQLを試してください:
select ur.roleList.organization from User ur where ur.id = 1
それはあなたに与えます List<Organization>
.
所属していません StackOverflow