Hibernate 命名查询 - 连接 3 个表
我有 3 个 bean:组织、角色、用户
角色 - 组织关系 - @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命名查询机制来实现这个? 谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@NamedQuery
我在
Organization
实体类上创建了以下@NamedQuery
。(我使用标准 JPA 注释,但我的提供程序是 Hibernate。)
测试
这是我运行的测试。
使用下面的表格和示例数据,此输出
请查看它是否适合您。
表格
(我使用的与你的有点不同,但应该不会太重要。)
示例数据
@NamedQuery
I've created the following
@NamedQuery
on theOrganization
entity class.(I used standard JPA annotations, but my provider was Hibernate.)
Test
This is the test I ran.
Using the tables and sample data below, this outputs
Please see if it works for you.
Tables
(I've used a bit different from yours, but it shouldn't matter too much.)
Sample data
尝试如下 HQL:
它将为您提供
List
。Try the HQL as below:
It will give you the
List<Organization>
.