gpt4 book ai didi

java - Hibernate - 标准查询

转载 作者:行者123 更新时间:2023-12-02 05:00:10 25 4
gpt4 key购买 nike

在我当前的项目中,我遇到了使用 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;

}

最佳答案

希望这对你有帮助..

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();

关于java - Hibernate - 标准查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45476377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com