Java JPA条件查询API和订单最后按空

Java JPA条件查询API和订单最后按空,java,jpa,criteria,Java,Jpa,Criteria,我的问题是空值必须是最后一个ORDERBY语句。下面是我的代码截图。我使用javax持久性标准生成器。我的问题很复杂 import javax.persistence.criteria.CriteriaBuilder; public Predicate getSomePredicate() { Predicate predicate = cb.conjunction();.... ...predicate.getExpressions().add(cb.and(cb.or(c

我的问题是空值必须是最后一个ORDERBY语句。下面是我的代码截图。我使用javax持久性标准生成器。我的问题很复杂

import javax.persistence.criteria.CriteriaBuilder;

public Predicate getSomePredicate() {
    Predicate predicate = cb.conjunction();....

    ...predicate.getExpressions().add(cb.and(cb.or(cb.and(v1, v2), cb.and(s1, s2))));

    EOrderByType orderType = EOrderByType.values()[orderBy]
            ;
    switch (orderType) {
    case PRICE: cq.where(predicate).orderBy(cb.asc(root.get("price")));
        break;
    case PRICE_HIGH_TO_LOW: cq.where(predicate).orderBy(cb.desc(root.get("price")));
        break;
    case CONSUPTION: cq.where(predicate).orderBy(cb.desc(root.get("consume")));
        break;
    default:
        break;
    }

    return cq.getRestriction();
}

如何使用criteria builder实现按零价格订购?

您好,我几乎搜索了所有互联网页面,然后找到了一个解决方案,您可以按部分编写交换机案例订单。如下所示:如果价格为空,则按desc订购,价格值为1000000;如果价格为空,则按asc订购,价格值为0。如果你想要这些,你可以写下面的表达式

                EOrderByType orderType = EOrderByType.values()[orderBy];                    
                Expression<Object> queryCase = cb.selectCase().when(cb.isNull(root.get("price")), 100000000).otherwise(root.get("price"));
                Direction dir = Direction.ASC;

                switch (orderType) {
                    case UCUZDAN_PAHALIYA:
                        queryCase = cb.selectCase().when(cb.isNull(root.get("price")), 100000000).otherwise(root.get("price"));
                        break;
                    case PAHALIDAN_UCUZA:
                        queryCase = cb.selectCase().when(cb.isNull(root.get("price")), 0).otherwise(root.get("price"));
                        dir = Direction.DESC;
                        break;
                }

                  cq.where(predicate).orderBy(direction( cb, queryCase, dir));
EOrderByType orderType=EOrderByType.values()[orderBy];
表达式queryCase=cb.selectCase().when(cb.isNull(root.get(“price”)),100000000)。否则(root.get(“price”);
方向dir=Direction.ASC;
开关(订单类型){
UCUZDAN_PAHALIYA案:
queryCase=cb.selectCase().when(cb.isNull(root.get(“price”)),100000000)。否则(root.get(“price”);
打破
帕哈利丹·乌库扎案:
queryCase=cb.selectCase().when(cb.isNull(root.get(“price”)),0)。否则(root.get(“price”);
dir=Direction.DESC;
打破
}
where(谓词).orderBy(方向(cb,queryCase,dir));

这是katsu对自己问题的回答的一点延伸。我试图找到一种解决方案,能够对表中允许某些列具有空值的大多数列进行排序。当按升序排序时,我希望将空值排序在最低非空值之前,当按降序排序时,将其排序在最低非空值之后。换句话说,与(Oracle的)默认行为几乎相反

我找到了其他可能做到这一点的方法,但这个方法不需要我跳出Hibernate和JPA2持久化,但仍然得到了我想要的结果。这是从我的实际代码中提取的代码片段,但合并在一个位置,并且更改了一些名称。您看到的任何语法、编译类型错误都可能是由此引起的

// sortByColumn is a String containing the Hibernate version of the column name, which had 
// been assigned as the ID of the table header column of the column by which we are sorting.

// sortAscending is a Boolean object containing Boolean.TRUE if we are to sort in ascending 
// order or Boolean.FALSE or null if we are to sort in descending order. This may seem a 
// bit odd, but in the case we need this for, the default sort column is a release date and 
// reverse chronological order is the most useful in that case.

// Also defined are: CriteriaQuery<SoftwareVersion> criteriaQuery and 
// CriteriaBuilder criteriaBuilder by the typical means.

final Root<SoftwareVersion> softwareVersionRoot = 
    criteriaQuery.from(SoftwareVersion.class);

private static final String EMPTY_STRING = "";

if (sortByColumn != null && sortByColumn.trim().length() > 0) {
  Order sortOrder;
  Expression<String> sortColumnExpression;
  if (sortByColumn.equalsIgnoreCase(SoftwareVersion_.installationFileLength.getName()) || 
      sortByColumn.equalsIgnoreCase(SoftwareVersion_.releaseTimestamp.getName())) {
    // The two non-String fields (exposed to the user) that we don't need to have the
    // lower() function operate upon.
    sortColumnExpression = oemSoftwareVersionRoot.get(sortByColumn);
  } else {
    // We use the lower() function to enforce case insensitive sorting on the columns we
    // show to the user, which are all Strings except as noted above.
    Expression<String> rootExpression = oemSoftwareVersionRoot.get(sortByColumn);
    sortColumnExpression = criteriaBuilder.lower(rootExpression);
  }

  // The columns for installation file name, installation file length and release timestamp
  // are just three of the columns that we allow the user to sort by. However, these three
  // may have null values in the database, and require some special handling.
  if (sortByColumn.equalsIgnoreCase(SoftwareVersion_.installationFileLength.getName()) || 
    sortByColumn.equalsIgnoreCase(SoftwareVersion_.installationFileName.getName()) ||
    sortByColumn.equalsIgnoreCase(SoftwareVersion_.releaseTimestamp.getName())
    ) {
    Expression<Object> queryCase;
    if (sortByColumn.equalsIgnoreCase(SoftwareVersion_.installationFileName.getName())) {
      // Installation file name is a (case insensitive) String
      queryCase = criteriaBuilder.selectCase().when(
        criteriaBuilder.isNull(sortColumnExpression), 
        StringUtil.EMPTY_STRING).otherwise(sortColumnExpression);
    } else if (sortByColumn.equalsIgnoreCase(SoftwareVersion_.releaseTimestamp.getName())) {
      // Release timestamp is a database timestamp
      LocalDateTime dateTime = LocalDateTime.of(1970,1,1,0,0); 
      // Equivalent to Unix epoch time. Note month is 1-12, not 0-11
      queryCase = criteriaBuilder.selectCase().when(
        criteriaBuilder.isNull(sortColumnExpression), 
          Timestamp.valueOf(dateTime)).otherwise(sortColumnExpression);
    } else {
      // Installation file length is a Long (or BigDecimal) computed when the file is uploaded.
      // The user can't set or change it, but can sort by it.
      queryCase = criteriaBuilder.selectCase().when(
        criteriaBuilder.isNull(sortColumnExpression), 
          Long.valueOf(0)).otherwise(sortColumnExpression);
    }

    if (asc != null && asc.booleanValue()) {
      sortOrder = criteriaBuilder.asc(queryCase);
    } else {
      sortOrder = criteriaBuilder.desc(queryCase);
    }
  } else {
    if (asc != null && asc.booleanValue()) {
      sortOrder = criteriaBuilder.asc(sortColumnExpression);
    } else {
      sortOrder = criteriaBuilder.desc(sortColumnExpression);
    }
  }
  criteriaQuery.orderBy(sortOrder);
}
//sortByColumn是一个字符串,包含列名的Hibernate版本,它具有
//已被指定为用于排序的列的表头列的ID。
//sortAscending是一个布尔对象,如果要按升序排序,则包含Boolean.TRUE
//order或Boolean。如果要按降序排序,则为FALSE或null。这似乎是一个错误
//有点奇怪,但在我们需要它的情况下,默认的排序列是发布日期和
//在这种情况下,逆时间顺序是最有用的。
//还定义了:CriteriaQuery CriteriaQuery和
//CriteriaBuilder CriteriaBuilder采用典型方法。
最终根目录softwareVersionRoot=
criteriaQuery.from(SoftwareVersion.class);
私有静态最终字符串为空_String=“”;
if(sortByColumn!=null&&sortByColumn.trim().length()>0){
排序器;
表达sortcolumexpression;
if(sortByColumn.equalsIgnoreCase(SoftwareVersion.installationFileLength.getName())||
sortByColumn.equalsIgnoreCase(SoftwareVersion\uU2.releaseTimestamp.getName()){
//两个非字符串字段(向用户公开),我们不需要
//lower()函数操作。
sortColumnExpression=oemSoftwareVersionRoot.get(sortByColumn);
}否则{
//我们使用lower()函数对我们使用的列强制执行不区分大小写的排序
//向用户显示,除上述内容外,所有字符串均为字符串。
表达式rootExpression=oemSoftwareVersionRoot.get(sortByColumn);
sortColumnExpression=criteriaBuilder.lower(rootExpression);
}
//安装文件名、安装文件长度和发布时间戳列
//我们只允许用户按其中三列进行排序
//数据库中可能有空值,并且需要一些特殊处理。
if(sortByColumn.equalsIgnoreCase(SoftwareVersion.installationFileLength.getName())||
sortByColumn.equalsIgnoreCase(SoftwareVersion.installationFileName.getName())||
sortByColumn.equalsIgnoreCase(SoftwareVersion\uU2.releaseTimestamp.getName())
) {
表达槲皮素酶;
if(sortByColumn.equalsIgnoreCase(SoftwareVersion\uuz.installationFileName.getName()){
//安装文件名是一个(不区分大小写)字符串
queryCase=criteriaBuilder.selectCase()。当(
criteriaBuilder.isNull(sortColumnExpression),
StringUtil.EMPTY_STRING),否则(sortcolumexpression);
}else if(sortByColumn.equalsIgnoreCase(SoftwareVersion\u.releasetimstamp.getName()){
//释放时间戳是数据库时间戳
LocalDateTime-dateTime=LocalDateTime.of(1970,1,1,0,0);
//相当于Unix纪元时间。注意月份是1-12,而不是0-11
queryCase=criteriaBuilder.selectCase()。当(
criteriaBuilder.isNull(sortColumnExpression),
Timestamp.valueOf(dateTime))。否则(sortcolumexpression);
}否则{
//安装文件长度是上载文件时计算的长(或BigDecimal)。
//用户不能设置或更改它,但可以按它排序。
queryCase=criteriaBuilder.selectCase()。当(
criteriaBuilder.isNull(sortColumnExpression),
Long.valueOf(0))。否则(sortColumnExpression);
}
如果(asc!=null&&asc.booleanValue()){
sortOrder=criteriaBuilder.asc(queryCase);
}否则{
sortOrder=criteriaBuilder.desc(queryCase);
}
}否则{
如果(asc!=null&&asc.booleanValue()){
sortOrder=criteriaBuilder.asc(sortclumnexpression);
}否则{
sortOrder=criteriaBuilder.desc(sortcolumexpression);
}
}
criteriaQuery.orderBy(sortOrder);
}

JPA不处理空值随订单的位置。您依赖于供应商的“提示”,不说