简体   繁体   English

Hibernate Criteria查询JOIN

[英]Hibernate Criteria Query JOIN

I have a problem where I have to write a criteria query to select records where there is a match between PK column in the selected table and the FK column in the table that is being used to JOIN along with some where clauses on the fields in the joined table. 我有一个问题,我必须编写一个条件查询来选择记录,其中所选表中的PK列与用于JOIN的表中的FK列以及该字段中的某些where子句之间存在匹配。加入表。

Entities used: Employee, Employee Assignment, SubCompany, Branch, Designation 使用的实体:员工,员工分配,子公司,分公司,指定

Entities used in JOIN : Employee and Employee Assignment JOIN中使用的实体:员工和员工分配

Description of Entities: An employee can be a part of a Big Company/Organization which is divided into sub companies where the employee can be assigned to. 实体描述:员工可以是大公司/组织的一部分,该公司/组织分为可分配员工的子公司。 Each employee can be assigned to multiple sub companies belonging to different branches with a different designation. 每个员工可以被分配到属于不同分支的多个子公司,具有不同的名称。

The relationship between Employee and EmployeeAssignment is oneToMany. Employee和EmployeeAssignment之间的关系是oneToMany。

SQL Query that needs to be written using Criteria API 需要使用Criteria API编写的SQL Query

    SELECT DISTINCT E.* FROM EMP E, EMP_ASSIGN EA
      WHERE
    E.ID = EA.EMP_ID AND (EA.SUB_COMP_ID IN (ACTUAL VALUES) 
    OR EA.BRANCH_ID IN (ACTUAL VALUES) 
    OR EA.DESG_ID IN (ACTUAL VALUES))

Employee.java Employee.java

    public class Employee{
         private String id;
         private String firstName;
         private String lastName;
         private Set<EmployeeAssignment> employeeAssignments;

         //getters and setters
     }

Employee.hbm.xml Employee.hbm.xml

   <hibernate-mapping>
     <class name = "Employee" table="EMP" >
        <id name="id" column="ID"/>
        <property name="firstName" column="FIRST_NAME"/>
        <property name="lastName" column="LAST_NAME"/>
        <set name="employeeAssignments" table="EMP_ASSIGN" inverse="true" fetch="join" cascade="save-update" lazy="true"
   where="trunc(SYSDATE) BETWEEN strt_dt and end_dt">
          <key>
            <column name="EMP_ID" not null="true"/>
          </key>
            <one-to-many notfound="ignore" class="com.someorganization.entity.EmployeeAssignment"/>
        </set>
    </class>
  </hibernate-mapping>

Employee Table 员工表

           CREATE TABLE EMP(
           ID VARCHAR(20) PRIMARY KEY,
           FIRST_NAME VARCHAR(30) NOT NULL,
           LAST_NAME VARCHAR(30) NOT NULL
           );

EmployeeAssignment.java EmployeeAssignment.java

           public class EmployeeAssignment{
           private String id;
           private String employeeId;
           private Date startDate;
           private Date endDate;
           private SubCompany company;
           private Branch branch;
           private Designation designation;

           //getters and setters
           }

EmployeeAssignment.hbm.xml EmployeeAssignment.hbm.xml

 <hibernate-mapping>
     <class name = "EmployeeAssignment" table="EMP_ASSIGN" >
           <id name="id" column="ID"/>
           <property name="employeeId" column="EMP_ID"/>
           <property name="startDate" column="STRT_DATE"/>
           <property name="endDate" column="END_DATE"/>

           <many-to-one name="subCompany" lazy="false" class="com.someorganization.entity.SubCompany">
            <column name="SUB_COMP_ID" not-null="false"/>
           </many-to-one>

           <many-to-one name="branch" lazy="false" class="com.someorganization.entity.Branch">
            <column name="BRANCH_ID" not-null="false"/>
           </many-to-one>

           <many-to-one name="designation" lazy="false" class "com.someorganization.entity.Designation">
           <column name="DESG_ID" not-null="false"/>
           </many-to-one>
     </class>
 </hibernate-mapping>

EmployeeAssignment Table EmployeeAssignment表

    CREATE TABLE EMP_ASSIGN(
    ID VARCHAR(20) PRIMARY KEY,
    EMP_ID VARCHAR(20) NOT NULL,
    STRT_DATE DATE,
    END_DATE DATE,
    SUB_COMP_ID VARCHAR(20) NOT NULL,
    BRANCH_ID VARCHAR(20) NOT NULL,
    DESG_ID VARCHAR(20) NOT NULL
    FOREIGN KEY(EMP_ID) REFERENCES EMP(ID),
    FOREIGN KEY(SUB_COMP_ID) REFERENCES SUB_COMP(ID),
    FOREIGN KEY(BRANCH_ID) REFERENCES BRANCH(ID),
    FOREIGN KEY(DESG_ID) REFERENCES DESG(ID))

SubCompany.java SubCompany.java

    public class SubCompany{
    private String id;
    private String subCompanyName;

      //getters and setters
    }

SubCompany.hbm.xml SubCompany.hbm.xml

    <hibernate-mapping>
      <class name = "SubCompany" table="SUB_COMP" >
        <id name="id" column="ID"/>
        <property name="subCompanyName" column="COMPANY_NAME"/>
      </class>
    </hibernate-mapping>

SubCompany Table 子公司表

     CREATE TABLE SUB_COMP(
       ID VARCHAR(20) PRIMARY KEY,
       COMPANY_NAME VARCHAR(30) NOT NULL
     );

Branch.java Branch.java

    public class Branch{
       private String id;
       private String branchName;

       //getters and setters
    }

Branch.hbm.xml Branch.hbm.xml

   <hibernate-mapping>
     <class name = "Branch" table="BRANCH">
       <id name="id" column="ID"/>
       <property name="branchName" column="BRANCH_NAME"/>
     </class>
   </hibernate-mapping>

Branch Table 分支表

    CREATE TABLE BRANCH(
      ID VARCHAR(20) PRIMARY KEY,
      BRANCH_NAME VARCHAR(30) NOT NULL
      );

Designation.java Designation.java

     public class Designation{
       private string id;
       private String designationName;

       //getters and setters
       }

Designation.hbm.xml Designation.hbm.xml

      <hibernate-mapping>
       <class name = "Designation" table="DESG">
         <id name="id" column="ID"/>
         <property name="designationName" column="DESG_NAME"/>
       </class>
      </hibernate-mapping>

Designation Table 指定表

      CREATE TABLE DESG(
        ID VARCHAR(20) PRIMARY KEY,
        DESG_NAME VARCHAR(30) NOT NULL
       );

I need help in writing the criteria query using the SQL provided. 我需要帮助使用提供的SQL编写条件查询。

Please check if criteria like this works. 请检查这样的标准是否有效。 I am setting up local instance to check, if you could test it and provide update that would be awesome. 我正在设置本地实例来检查,如果你可以测试它并提供非常棒的更新。

Criteria criteria = session.createCriteria(Employee.class)
                .createAlias(“employeeAssignments.id” , “emp_id”)
                .createAlias(“employeeAssignments.subCompany” , “subCompany”)
                .createAlias(“subCompany.id” , “subCompany_id”)
                .createAlias(“employeeAssignments.branch” , “branch”)
                .createAlias(“branch.id” , “branch_id”)
                .createAlias(“employeeAssignments.designation” , “designation”)
                .createAlias(“designation.id” , “designation_id”);
Criterion em_assig_id = Resitriction.eq("emp_id”, 601);
Criterion id = Restriction.eq(“id”, 601);
Disjunction or = Restrictions.disjunction();
Or.add(Restriction.in(“subCompany_id”, new String[] {“”,””,””} ));
Or.add(Restriction.in(“branch_id”, new String[] {“”,””,””} ));
Or.add(Restriction.in(“designation_id”, new String[] {“”,””,””} ));
Criterion final = Restriction.and(or, Restriction.and(em_assig_id, id));
criteria.add(final);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM