我使用JPA条件API从日期库中获取记录。 我有实体记录,字段 dateTime ,可以为null。我会编码:
public List<Record> find(RecordFilter recordFilter, int page, int pageSize) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Record> criteriaQuery = criteriaBuilder.createQuery(Record.class);
Root<Record> recordRoot = criteriaQuery.from(Record.class);
/*
* JOINS. Left Joins are used for optional fields, or fields inside of the optional fields.
*/
Join<Record, Agency> recordAgencyJoin = recordRoot.join(RecordTable.FIELD_AGENCY);
//Some other joins
//This is where I had the problem.
applyOrderBy(criteriaQuery, criteriaBuilder, recordRoot);
/*
* Specify which columns to select and their order.
* criteriaQuery.multiselect(....);
*/
applyMultiSelect(recordRoot, recordAgencyJoin, /*other joins*/ criteriaQuery);
/*
* criteriaQuery.where(somePredicate);
*/
applyFilter(recordFilter, criteriaQuery, criteriaBuilder,
recordRoot, recordAgencyJoin /*, other joins*/);
TypedQuery<Record> query = entityManager.<Record>createQuery(criteriaQuery);
RepositoryUtils.applyPagination(query, page, pageSize);
return query.getResultList();
}
private void applyOrderBy(CriteriaBuilder criteriaBuilder, Root<Record> recordRoot, CriteriaQuery<Record> criteriaQuery) {
//Other fields to be added to the final sort.
Order dateTimeDescOrder = criteriaBuilder.desc(recordRoot.get(RecordTable.FIELD_DATE_TIME));
criteriaQuery.orderBy(dateTimeDescOrder /*, other orders by*/);
}
事实证明,首先显示带有NULL dateTimeField的记录。 我使用Postrgres数据库。 我会回答这个问题,因为我找到了解决方案。 这是一篇类似的帖子。 JPA Criteria Query API and order by null last
答案 0 :(得分:9)
我在这里回答了这个任务。
首先,Postgres默认首先返回空值。
SELECT * FROM record ORDER BY date_time_field DESC;
https://stackoverflow.com/a/7621232/4587961
SELECT * FROM record ORDER BY date_time_field DESC NULLS LAST;
其次,我必须更改 applyOrderBy 方法
private void applyOrderBy(CriteriaBuilder criteriaBuilder, Root<Record> recordRoot, CriteriaQuery<Record> criteriaQuery) {
//In the class code
//private static final Date MIN_DATE = new Date(0L);
final Date MIN_DATE = new Date(0L);
//We treat records will NULL dateTimeField as if it was MIN_DATE.
Order dateTimeDescOrder = criteriaBuilder.desc(
//NULL values - last - WORKAROUND.
criteriaBuilder.coalesce(recordRoot.get(RecordTable.FIELD_DATE_TIME), MIN_DATE));
criteriaQuery.orderBy(dateTimeDescOrder);
}
注意,来自hibernate-jpa-2.1的 CriteriaBuilder 。
/**
* Create an expression that returns null if all its arguments
* evaluate to null, and the value of the first non-null argument
* otherwise.
*
* @param x expression
* @param y value
*
* @return coalesce expression
*/
<Y> Expression<Y> coalesce(Expression<? extends Y> x, Y y);
答案 1 :(得分:4)
JPA规范中没有任何内容可以控制NULLS的处理方式(并且所有RDBMS都优先使用默认值)。这在JPQL(基于字符串的查询)或Criteria API中都不可用。使用JPQL,所有主要的JPA提供程序都允许使用
NULLS [FIRST|LAST]
在订购条款中。对于Criteria,您受到Criteria API的约束,因此无法做到。我知道DataNucleus JPA提供了JPA Criteria API的自定义版本,允许指定
Order order = criteriaBuilder.asc(myExpression).nullsFirst();
但很明显,这是特定于该JPA提供者。
答案 2 :(得分:1)
在JPA或Eclipselink中没有任何东西可以在CriteriaBuilder中使用案例。
示例:
Order order1 = cBuilder.desc(cBuilder.selectCase().when(cBuilder.isNotNull(from.get('ColumnName1')), from.get('ColumnName1')).otherwise(from.get('ColumnName1')));
CQuery.orderBy(order1);
它将为您提供最佳答案
答案 3 :(得分:0)
我将JPA与eclipselink和Postgres一起使用,并且这里的namedquery可以正常工作。 这是摘录:
,@NamedQuery(name = "ServerPasswords.forTheServer",
query = "SELECT ss FROM ServerPasswords ss WHERE ss.fkIds = :IDServer AND ss.private = FALSE "
+ "ORDER BY ss.servce, ss.active DESC, ss.datefrom DESC NULLS LAST, ss.dateto NULLS LAST")