2017-08-03 151 views
3

在我當前的項目中,我遇到了使用hibernate條件查詢獲取實體的問題。我有員工和任務實體。休眠 - 條件查詢

任務實體:

@Entity 
@Table(name="task") 
public class Tasks implements Serializable { 

private static final long serialVersionUID = -7988799579036225137L; 

@ManyToOne 
@JoinColumn(name="employee_id") 
private Employees employees; 

public Employees getEmployees() { 
    return employees; 
} 

****relevant getters & setters****** 

僱員實體:

@Entity 
@Table(name="employee") 
public class Employees implements Serializable { 

private static final long serialVersionUID = -7988799579036225137L; 

@Id 
@Column(name="employee_id") 
private String empId; 

****relevant getters & setters****** 

我試圖從表單設置EMPID和有關僱員顯示的相關任務。我必須對標準API給予此限制。我目前的代碼如下。我想知道我該怎麼做。請給我一個幫助。提前致謝。

ViewAssignedTasksDaoImpl.java

@Repository 
@Transactional 
public class ViewAssignedTasksDaoImpl implements ViewAssignedTasksDao { 

@Autowired 
private HibernateUtilImpl hibernateutilimpl; 

@Resource(name = "sessionFactory") 
protected SessionFactory sessionFactory; 


public List<Tasks> viewassignedTasks(Tasks tasks) { 


    Session session = sessionFactory.openSession(); 

    Employees employees = tasks.getEmployees(); 

    Criteria cr = session.createCriteria(Tasks.class); 
    cr.add(Restrictions.eq("empId", tasks.employees.getEmpId())); 
    List<Tasks> tasksList = cr.list(); 

return tasksList; 

} 
+1

什麼問題你面對? – sunkuet02

+0

所有行都沒有限制地返回。 – jsb9009

+0

只是檢查您的tasks.empoyees.getEmpId()返回正確的值..更好的方式來記錄和檢查 –

回答

0

希望這將幫助你..

ArrayList<Tasks> TasksListByEmpId = (ArrayList<Tasks>) this 
         .getSession() 
         .createCriteria(Tasks.class,"task") 
         .setProjection(Projections.projectionList() 
     .add(Projections.property("task.yourproprty_name"), "yourproprty_name") 
     .add(Projections.property("task.yourproprty_name"), "yourproprty_name") 
     .add(Projections.property("task.yourproprty_name"), "yourproprty_name") 
     .add(Projections.property("task.yourproprty_name"), "yourproprty_name")) 
         .add("empId", tasks.employees.getEmpId())) 
         .add(Restrictions.in("task.taskStatus", statusList)) 
         .setResultTransformer(Transformers.aliasToBean(Tasks.class)) 
         .list();