当前位置: 首页 > 知识库问答 >
问题:

JPA标准查询API和订单(按空)

祁承嗣
2023-03-14

我的问题是空值必须是最后一个order by语句。下面是我的代码截图。我使用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以零价格完成订单?

共有2个答案

裴金鑫
2023-03-14

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

我找到了其他可能做到这一点的方法,但这个方法不需要我跳出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);
}
王高超
2023-03-14

嗨,我几乎搜索了所有的网页,然后找到了一个解决方案,你可以写开关箱订单的一部分。如下所示:如果价格为空,则按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));
 类似资料:
  • 我被一个简单的问题困住了;如何在ed实体上调用。基本上,我正试图通过JPA标准实现以下目标: 我有以下几点: 如何通过d.name,c.name使用来实现

  • 我有2个班: 我试图创建一个带有动态order by和参数的查询,例如:我想在DrcommentsPK时选择所有Drcomments记录。commentPrimaryCode等于1,其顺序为DrcommentsPK。注释第二代码。这就是我尝试的: 问题是我得到的结果列表没有按commentSecondaryCode desc顺序排序。。 我做错什么了吗?如何做到这一点?如何创建按eMedTable

  • 我使用JPA criteria API从数据库中获取记录。我有一个实体记录,其字段dateTime可以为空。我会编码: 事实证明,dateTimeField为空的记录首先显示。我使用Postrgres数据库。我会回答这个问题,因为我找到了解决办法。这里有一个类似的帖子。JPA标准查询API和订单(按空)

  • 可以在Java中使用criteria query或jpql进行自定义排序吗?在我的表中,我有一个列-数字类型(目前,该列包括,,,,)。我想按以下顺序对结果排序:,,,,。 我怎么能做到呢?

  • 我有两个遗留实体 和由。我有一个CriteriaAPI查询,它返回所有者集合,我需要按。 因为我不能将子查询放到,也不能更改这些实体的JPA映射,所以对我来说似乎并不容易。 请给任何想法。

  • 说明 微信支付查询订单SDK。 官方文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_2 类 请求参数类 请求参数 类名:\Yurun\PaySDK\Weixin\OrderQuery\Request 属性 名称 类型 说明 $_apiMethod string 接口名称 $transaction_id string