JpaSort 在 Spring Data 中使用“nulls last" [英] JpaSort using 'nulls last' with Spring Data

查看:43
本文介绍了JpaSort 在 Spring Data 中使用“nulls last"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Spring Data 中使用 JpaSort 最后返回空值?

How do I use JpaSort in Spring Data to return nulls last?

我目前使用这种结构进行排序:

I currently use this kind of construct for my sorting:

new JpaSort( JpaSort.path( Datasheet_.componentSubtype )
                    .dot( ComponentSubtype_.componentType )
                    .dot( ComponentType_.name ) )
                    .and( new JpaSort( JpaSort.path( Datasheet_.componentSubtype )
                                              .dot( ComponentSubtype_.name ) ) )

我在这个答案中看到 Hibernate critaria API 确实允许它,但想使用 JPA.>

I saw in this answer that the Hibernate critaria API does allow it, but would like to use JPA.

推荐答案

简短的回答是:你不能,因为 JPA(阅读:Criteria API 和 JPQL)没有定义方法来定义 nulls.

The short answer is: you can't, as JPA (read: neither Criteria API nor JPQL) doesn't define means to define the sort ordering for nulls.

您可能想考虑使用 Querydsl 作为我们的 匹配抽象 公开了必要的 API 来定义它.但是请注意,Querydsl 对生成的 JPQL 使用持久性提供程序特定的扩展

You might wanna look into using Querydsl as an alternative as our matching abstraction exposes the necessary API to define this. Note however, Querydsl uses persistence provider specific extensions to the JPQL generated

这篇关于JpaSort 在 Spring Data 中使用“nulls last"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆