关键词:
【中文标题】Spring data JPA nativeQuery order by 无效【英文标题】:Spring data JPA nativeQuery order by is invalid 【发布时间】:2019-01-27 11:19:02 【问题描述】:Spring Data Jpa 方法如下:
@Query("select pb.id,pp.max_borrow_amt,pp.min_borrow_amt
from product_loan_basic pb left join product_loan_price pp on pb.code=pp.product_code
where pb.code IN(?1) and pb.status='publish' order by ?2 ",
nativeQuery = true)
List<Object[]> findByCodesIn(List<String> codes,String orderby);
那么 order by 是“max_borrow_amt desc”,但这是无效的。 列表是无序的。
【问题讨论】:
您不能在准备好的语句中将顺序设置为参数 【参考方案1】:Spring Data JPA 中的动态排序
如果您使用 JPA 查询,您可以使用 Sort 作为查询方法的参数来定义排序顺序:
@Query("select m from Model m")
List<Model> getSortedList(Sort sort);
然后,例如:
List<Model> models = getSortedList(Sort.by(Sort.Direction.DESC, "name"));
但是带有原生查询的 Spring Data JPA can't use Sort
:
Spring Data JPA 目前不支持本地查询的动态排序,因为它必须操作声明的实际查询,而对于本地 SQL,它不能可靠地做到这一点。
但是,您可以改用Pageable 及其实现PageRequest:
@Query(value = "select m.name as name from models m", nativeQuery = true)
List<ModelProjection> getSortedList(Pageable p);
然后:
List<ModelProjection> modelNames = getSortedList(PageRequest.of(0, 1000, Sort.Direction.DESC, "name"));
附:而不是Object
s的数组作为返回参数,最好使用projections,例如:
public interface ModelProjection
String getName();
请注意,在这种情况下,好的做法是在查询中使用别名(即m.name as name
)。它们必须与投影中对应的 getter 匹配。
工作demo 和test。
【讨论】:
【参考方案2】:谢谢大家! 我的问题已经解决了。
如果你想使用 Spring data jpa nativeQuery
& Sort,你应该这样做:
@Query(
value ="select pb.id,pp.max_borrow_amt from product_loan_basic pb left join product_loan_price pp on pb.code=pp.product_code ORDER BY ?##pageable ",
countQuery = "select count(*) from product_loan_basic",
nativeQuery = true
)
Page<Object[]> findAllProductsAndOrderByAndSort(Pageable pageable);
?##pageable
是必需的,countQuery
是必需的。
Pageable pageable = new PageRequest(0,1000,Sort.Direction.DESC,"id");
然后对结果进行排序。
见Spring Data and Native Query with pagination。
【讨论】:
见Spring Data and Native Query with pagination 在最新版本的 Spring Boot 中,没有必要对?##pageable
使用这些技巧。请参阅我的demo 及其test。Spring-data-jpa LazyInitializationException:没有会话
】Spring-data-jpaLazyInitializationException:没有会话【英文标题】:Spring-data-jpaLazyInitializationException:noSession【发布时间】:2014-05-2212:29:35【问题描述】:我的Spring-data-jpa项目有问题。JavaConfig文件...@Configuration@EnableJpaRepositories("it.my 查看详情
Spring-data-jpa:批量插入不起作用
】Spring-data-jpa:批量插入不起作用【英文标题】:Spring-data-jpa:batchinsertsarenotworking【发布时间】:2021-03-0909:13:38【问题描述】:我使用这个article为我的一个实体使用spring-data-jpa实现批量插入。我正在使用MSSQLDB。我将主键生成策... 查看详情
学习spring-data-jpa(二十)---@enablejparepositories
1、在使用SpringBoot项目的时候,我们只需要添加spring-boot-starter-data-jpa的依赖就可以使用了。其实是spring-boot-autoconfigure帮我们做好了,在该jar包下的META-INF中spring.factories中可以找到org.springframework.boot.autoconfigure.data.jpa.JpaRepositori 查看详情
Spring Data JPA 是 JPA 实现吗?
】SpringDataJPA是JPA实现吗?【英文标题】:IsSpringDataJPAaJPAimplementation?【发布时间】:2016-08-2313:58:27【问题描述】:我正在尝试“真正”理解SpringFramework。我对SpringCore(DI)和SpringMVC有一定的了解。对于数据部分,我现在专注于SpringDa... 查看详情
Spring-data-jpa 投影生成查询不正确
】Spring-data-jpa投影生成查询不正确【英文标题】:Spring-data-jpaprojectiongeneratingqueryincorrectly【发布时间】:2016-08-0721:09:26【问题描述】:我制作了一个使用spring-data-jpa投影功能的示例项目,但它生成的查询不正确。它不是仅获取声... 查看详情
spring-data-jpa 1.11.16 带游标的存储过程
】spring-data-jpa1.11.16带游标的存储过程【英文标题】:spring-data-jpa1.11.16storedprocedurewithcursor【发布时间】:2019-05-1210:19:24【问题描述】:我无法在spring-data-jpa(版本1.11.16)和oracle服务器中使用带有游标的存储过程。存储库:@Reposit... 查看详情
Spring-data-jpa + Hibernate 未创建预期表
】Spring-data-jpa+Hibernate未创建预期表【英文标题】:Spring-data-jpa+Hibernatenotcreatingexpectedtable【发布时间】:2017-05-2915:58:42【问题描述】:在您认为这是重复之前,我知道这些答案(以及其他):HibernatenotcreatingTableautomaticallyinspringboo... 查看详情
Spring Boot 自动配置无法与 spring-data-jpa 一起正常工作
】SpringBoot自动配置无法与spring-data-jpa一起正常工作【英文标题】:Springbootautoconfigurationnotworkingproperlywithspring-data-jpa【发布时间】:2019-08-0422:52:57【问题描述】:我有一个小型SpringBoot应用程序(它只是一个概念证明),它针对H2... 查看详情
用于 Spring-Data-JPA 注释的 setMaxResults?
】用于Spring-Data-JPA注释的setMaxResults?【英文标题】:setMaxResultsforSpring-Data-JPAannotation?【发布时间】:2012-03-0800:23:26【问题描述】:我正在尝试将Spring-Data-JPA合并到我的项目中。让我困惑的一件事是如何通过注释实现setMaxResults(n)... 查看详情
Spring -data-jpa ,存储库类不起作用
】Spring-data-jpa,存储库类不起作用【英文标题】:Spring-data-jpa,TheRepositoryClassdoesn`twork【发布时间】:2019-03-0214:31:13【问题描述】:我正在Springboot中开发一个应用程序,使用Mysql5、spring-data-jpa、Java8...问题是我无法部署它,因为“... 查看详情
没有 Spring Boot 的 Spring Data JPA
】没有SpringBoot的SpringDataJPA【英文标题】:SpringDataJPAwithoutSpringBoot【发布时间】:2014-07-2108:57:58【问题描述】:SpringBoot不适用于GoogleAppEngine(至少不适合我)。无论GitHub或其他存储库中编写或提供的许多示例都使用SpringBoot,是... 查看详情
Spring Data JPA 忽略空参数
】SpringDataJPA忽略空参数【英文标题】:SpringDataJPAignorenullparameter【发布时间】:2019-07-0219:40:29【问题描述】:假设我有以下JPA方法:publicList<FrequencyCode>findAllByNameContainingAndAllowExplicitDosingTimesEqualsOrderByName(Stringname,Booleana 查看详情
spring_data_jpa以及hibernate简单使用
首先在pom.xml添加依赖<!--https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa--><dependency><groupId>org.springframework.data</groupId><artifactId>spring- 查看详情
使用 spring-data-jpa 自定义 ItemReader
】使用spring-data-jpa自定义ItemReader【英文标题】:CustomItemReaderwithspring-data-jpa【发布时间】:2016-09-1011:38:47【问题描述】:我正在使用现有实体和存储库创建一个Spring批处理项目。对于使用现有jpa存储库读取数据的作业,我需要使... 查看详情
Spring Data JPA - 无法重用参数
】SpringDataJPA-无法重用参数【英文标题】:SpringDataJPA-Unabletoreuseparameter【发布时间】:2018-07-1914:32:03【问题描述】:我正在尝试使用spring-data-jpa来选择一个包含所有提供的参数值的实体。我的实体如下所示:@Entity@Table(name="books",s... 查看详情
Spring Boot + Spring Data JPA + 事务无法正常工作
】SpringBoot+SpringDataJPA+事务无法正常工作【英文标题】:SpringBoot+SpringDataJPA+Transactionsnotworkingproperly【发布时间】:2015-04-2019:20:50【问题描述】:我使用1.2.0版本和spring-boot-starter-data-jpa创建了一个SpringBoot应用程序,并且我正在使用... 查看详情
Spring Data JDBC / Spring Data JPA 与 Hibernate
】SpringDataJDBC/SpringDataJPA与Hibernate【英文标题】:SpringDataJDBC/SpringDataJPAvsHibernate【发布时间】:2017-07-1703:21:54【问题描述】:选择的典型现实生活场景是什么?我想了解这两种实现中最适合的场景。【问题讨论】:你提到***.com/que... 查看详情
如何在 Spring Data (JPA) 派生查询中按多个属性排序?
】如何在SpringData(JPA)派生查询中按多个属性排序?【英文标题】:HowtosortbymultiplepropertiesinSpringData(JPA)derivedqueries?【发布时间】:2014-10-1209:50:26【问题描述】:我正在查看此页面(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/... 查看详情