使用JoinTable的ManyToMany关系中的JPQL问题

时间:2018-05-07 14:12:28

标签: hibernate jpa jpql

我有三个实体。我希望使用JPA

获取DEP ID的RULE列表

RULE ---------- ----------一对多> EMP ------------多对多--------- ---> DEP。

 @Entity
    @Table(name = "EMP")
    public class Emp implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;


        @OneToMany(mappedBy = "emp")
        private List<Rule> rules;

        @ManyToMany
        @JoinTable(name = "rule_ref_sub_category_ref", joinColumns = {
                @JoinColumn(name = "EMP_ID") }, inverseJoinColumns = { @JoinColumn(name = "DEP_REF_ID") })
        private List<Dep> Deps;
    }


     */
    @Entity
    @Table(name="DEP")
    public class Dep implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long id;

        @Column(name="LABEL")
        private String label;

        private int mandatory;

        @ManyToMany(mappedBy="Deps")
        private List<Emp> emps;

    }

    @Entity
    @Table(name="RULES")
    public class Rule implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long id;


        @ManyToOne
        @JoinColumn(name="EMP_REF_ID")
        private Emp emp;
    }

someOne可以指导我提供一个样本JPQL请求来解决此问题。

1 个答案:

答案 0 :(得分:0)

这是一个非常简单的查询

select r from rule r
join r.emp emp
join emp.Deps dep
where dep.id = :depId

阅读docs的一些内容以了解可能的连接语法形式