在我目前的项目中,我遇到了使用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;
}
答案 0 :(得分: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();