如何在子查询的JPQL查询中使用In子句

时间:2018-01-30 05:06:22

标签: java hibernate jpa persistence jpql

我有一个实体类,如下所示

ExportMO.java

@Entity
@Table(name = "DATAEXPORTTEMPLATE")
@NamedQueries({
    @NamedQuery(name = "ExportTemplateMO.getTemplatesByUser", query = "SELECT DISTINCT template FROM ExportTemplateMO template " +
        "join template.fields exportFields WHERE template.eId IN (SELECT S1.dcid FROM SchoolMO S1 join S1.fields S2 WHERE S2.dcid = :userDCID)")
})
public class ExportMO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "DATATEMPLATEID", unique = true, nullable = false)
    private Long templateId;


    @Column(name = "EID", nullable = true)
    private Long eId;

    @OneToMany(fetch=FetchType.EAGER, mappedBy="templateMO", cascade={CascadeType.ALL}, orphanRemoval=true)
    private Set<ExportFieldMO> fields;

}

SchoolMO.java

    @Entity
    @Table(name = "SCHOOLSTAFF")

    public class SchoolMO implements Serializable {
        @Id
        @Column(name = "DCID", unique = true, nullable = false)
        private Long dcid;

        @Column(name = "ID", unique = true) 
        private Long id;

        @Column(name = "USERS_DCID", unique = false, nullable = false)    
 @JoinColumn(name="USERS_DCID",referencedColumnName="USERS_DCID",nullable=false)
            private Long users_dcid;

        @OneToMany(fetch=FetchType.EAGER, mappedBy="users_dcid", cascade={CascadeType.ALL}, orphanRemoval=true)
    private Set<SchoolStaffLeanMO> fields;
    }

有人可以帮助我使用子句面的IN子句来使用它。 如果我尝试使用相同的'='而不是'IN',我就能得到结果。

提前致谢。

0 个答案:

没有答案