gpt4 book ai didi

java - session.getCurrentSession().createCriteria(MyClass.class) 已弃用如何使用 .addOrder() 现在

转载 作者:行者123 更新时间:2023-11-30 05:47:23 24 4
gpt4 key购买 nike

我一直在开发:

public List<DepartmentType> getAllDepartmentTypes() {
return session.getCurrentSession()
.createCriteria(DepartmentType.class)
.addOrder(Order.asc("department_type_name"))
.list();
}

地点:

@Autowired
SessionFactory session;

来自this Question关于 createCriteria() 的替代方案,我创建了一个获取列表的方法:

public List<DepartmentType> getAllDepartmentTypes() {
CriteriaBuilder builder = session.getCurrentSession().getCriteriaBuilder();
CriteriaQuery<DepartmentType> criteriaQuery = builder.createQuery(DepartmentType.class);
return session.getCurrentSession()
.createQuery(criteriaQuery)
.getResultList();
}

但我还是不知道在哪里使用:

addOrder(Order.asc("department_type_name"))

最佳答案

您需要使用CriteriaQuery.orderBy()提供 order by 子句。

public List<DepartmentType> getAllDepartmentTypes() {
CriteriaBuilder builder = session.getCurrentSession().getCriteriaBuilder();
CriteriaQuery<DepartmentType> criteriaQuery = builder.createQuery(DepartmentType.class);
Root<DepartmentType> deptType = criteriaQuery.from(DepartmentType.class);
criteriaQuery.select(deptType).orderBy(builder.asc(deptType.get("department_type_name")));
return session.getCurrentSession()
.createQuery(criteriaQuery)
.getResultList();
}

关于java - session.getCurrentSession().createCriteria(MyClass.class) 已弃用如何使用 .addOrder() 现在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54615252/

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