2016-06-28 68 views
0

Hibernate查詢我想打一個Hibernate查詢,其中我想爲下面的SQL查詢

我越來越多員工ID的單leaveId

我設計工作MySQL查詢以下:

select * from lms_employee_leave_records 
     where (employee_id=2633 or 
       employee_id=2634 or employee_id=2635) 
       and leave_category_id=1; 

我已經做了簡單的查詢: 我應當怎​​樣查詢,因爲有可能是N無。查詢中的員工數,但假身份證只有一個。那麼,我設計的是:

criteria.add(Restrictions.and(Restrictions.or(c1,c2), c3)); 

但問題是,在OR,我不能夠設置多個員工ID爲:

Restrictions.eq("employee.employeeId", employee.getEmployeeId()) 
+0

你可以使用相同的本地查詢,如果你想..休眠吃了會照顧到你的實體類的映射..你有沒有嘗試過什麼? – Zulfi

+0

如何將其用作原生查詢? –

+0

session.createSQLQuery() – Zulfi

回答

0

首先,你需要創建實體映射你的表lms_employee_leave_records作爲EmployeeRecords

更多關於此映射 https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html

(當你創建ÿ我承擔OU的實體,您將使用成員名稱是相同的表中的列名)

與Hibernate原生API,你將有兩個選擇

HQL

下面是HQL查詢你可以使用

from EmployeeRecords as e 
    where e.employee_id in (2633,2634,2635) 
      and e.leave_category_id=1; 

通過在會話中使用的createQuery()執行上面的查詢

更多關於HQL這裏

https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html

標準

下面是你在條件查詢

session.createCriteria(EmployeeRecords.class) 
.add(Restrictions.in("employee_id",list)) //list will have list of emp ids 
.add(Restrictions.eq("leave_category_id",1)) 
.list() 

更多關於此標準

https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querycriteria.html

編輯

取而代之的是

criteria.add(Restrictions.and(Restrictions.or(c1,c2), c3)); 

你能做到這一點

Criterion c1=Restrictions.eq("employee_Id", empId1); 
    Criterion c2=Restrictions.eq("employee_Id", empId2); 
    Criterion c3=Restrictions.eq("employee_Id", empId1); 

    Criterion or=Restrictions.or(c1,c2,c3); 

    session.createCriteria(EmployeeRecords.class) 
    .add(or) 
    .add(Restrictions.eq("leave_category_id",1)) 
    .list() 

empId1,empId2,empId3將是你的變量保持EMP IDS

+0

。 –

+0

我應該做什麼查詢,因爲有可能是N否。查詢中的員工數,但假身份證只有一個。所以我設計的是: criteria.add(Restrictions.and(Restrictions.or(c1,c2),c3)); 但問題是,在OR中,我無法設置多個員工ID: Restrictions.eq(「employee.employeeId」,employee。getEmployeeId()) –

+0

看到我編輯的答案 – Zulfi

0
List al = new ArrayList(); 
         al .add(2633); 
         al.add(2634); 
         al.add(2635); 
         Criteria ct = getSession().createCriteria(EmployeeLeaveRecords.class) 
           .add(Restrictions.eq("leave_category_id",1)) 
           .add(Restrictions.in("employee_id", al));