Spring Boot JPA中的自定义查询问题

     2023-03-16     287

关键词:

【中文标题】Spring Boot JPA中的自定义查询问题【英文标题】:Problem with custom Query in Spring boot JPA 【发布时间】:2020-07-05 02:06:49 【问题描述】:

我正在尝试按范围计算记录数,但在运行服务时出现错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '<' near line 1, column 58 [SELECT new net.BBB.ProjectB.entity.AgeCount(COUNT(s.edad < 15 OR NULL), COUNT(s.edad < 25 AND s.edad > 16 OR NULL), COUNT(s.edad < 35 AND s.edad > 26 OR NULL), COUNT(s.edad < 45 AND s.edad > 36 OR NULL), COUNT(s.edad > 46 OR NULL),FROM net.BBB.ProjectB.entity.Student AS s) ]

当我在数据库中执行查询时,它运行良好。我的存储库的代码是

package net.BBB.ProjectB.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import net.BBB.ProjectB.entity.AgeCount;
import net.BBB.ProjectB.entity.NacionalityCount;
import net.BBB.ProjectB.entity.Student;

@Repository
public interface StudentRepository extends CrudRepository<Student, Long> 

    List<Student> findByNombre(String nombre);

    @Query(value = "SELECT new net.BBB.ProjectB.entity.NacionalityCount(COUNT(s.nacionalidad), s.nacionalidad) "
            + "FROM Student AS s GROUP BY s.nacionalidad")
    List<NacionalityCount> countByNacionalidad();

    @Query(value = "SELECT new net.BBB.ProjectB.entity.AgeCount(COUNT(s.edad < 15 OR NULL), "
            + "COUNT(s.edad < 25 AND s.edad > 16 OR NULL), COUNT(s.edad < 35 AND s.edad > 26 OR NULL), "
            + "COUNT(s.edad < 45 AND s.edad > 36 OR NULL), COUNT(s.edad > 46 OR NULL),"
            + "FROM Student AS s) ")
    List<AgeCount> countByAge();


第一个查询工作正常,第二个是问题。以防万一,实体的名称是 Student,它有一个名为“edad”的属性,我试图按范围计数。我已经尝试过其他适用于数据库的查询,例如:

SELECT COUNT(s.edad BETWEEN 0 AND 16 OR NULL), 
        COUNT(s.edad BETWEEN 17 AND 25 OR NULL), 
        COUNT(s.edad BETWEEN 26 AND 35 OR NULL),
        COUNT(s.edad BETWEEN 35 AND 45 OR NULL)
FROM students AS s;

显然带有实体的名称。我不知道是否有办法让它发挥作用。

【问题讨论】:

【参考方案1】:

不完全确定,但在您的 HQL 查询中,您在末尾有一个“,”加上一个“)”,它应该在其他地方吗?

  + "COUNT(s.edad < 45 AND s.edad > 36 OR NULL), COUNT(s.edad > 46 OR NULL),"
            + "FROM Student AS s) ")

一般来说,我会尝试逐步修复此查询。 IE。将所有计数替换为静态数字,然后将 1 个计数替换为实数表达式,一旦可行,替换下一个计数,等等。

【讨论】:

谢谢,没有注意到,已经更正了,但错误仍然存​​在。我在这里发现了一个类似的问题link,因为它说“查询解析器似乎不支持在 count(...) 中调用另一个函数”,所以我不知道如何编写这个查询。跨度>

带有分页的 Spring JPA 存储库中的自定义查询

】带有分页的SpringJPA存储库中的自定义查询【英文标题】:CustomQueryinSpringJPARepositorywithPagination【发布时间】:2015-10-2916:53:32【问题描述】:我已经尝试使用SpringBoot实现JPA存储库,它工作正常。现在,如果我尝试在使用@QueryAnnotat... 查看详情

JPQL 的 Spring Boot JPA“查询验证失败”错误

】JPQL的SpringBootJPA“查询验证失败”错误【英文标题】:SpringBootJPA"Validationfailedforquery"errorforJPQL【发布时间】:2018-02-2305:48:11【问题描述】:我正在尝试使用jpa的自定义查询(不是nativeQuery,因为我想将结果映射到自定义... 查看详情

Spring Boot 中的自定义异常

】SpringBoot中的自定义异常【英文标题】:CustomExceptioninSprinBoot【发布时间】:2019-12-1209:54:06【问题描述】:我在SPRINGBOOT中编写了以下自定义错误处理程序@RestControllerAdvicepublicclassCustomGlobalExceptionHandlerextendsResponseEntityExceptionHandler@... 查看详情

带有 Java/Spring Boot 的 GraphQL 无法从传递的查询中选择定义为在模式中的字段上定义的自定义指令

】带有Java/SpringBoot的GraphQL无法从传递的查询中选择定义为在模式中的字段上定义的自定义指令【英文标题】:GraphQLwithJava/SpringbootunabletopickcustomdirectivedefinedasonFieldinschemafrompassedquery【发布时间】:2021-08-1120:42:52【问题描述】:已... 查看详情

Spring Data JPA - JpaRepository 中的自定义排序

】SpringDataJPA-JpaRepository中的自定义排序【英文标题】:SpringDataJPA-CustomSortinJpaRepository【发布时间】:2016-01-2322:18:39【问题描述】:我正在将SpringDataJPA与SpringDataREST一起使用,并且我为我的Thing实体创建了一个JpaRepository。@Repositoryp... 查看详情

Spring Boot REST/JPA 服务中的不相等查询

】SpringBootREST/JPA服务中的不相等查询【英文标题】:NotEqualsQueryonSpringBootREST/JPAService【发布时间】:2020-05-0821:33:44【问题描述】:我是SpringBoot的新手,我创建了一个使用JPA的非常基本的REST服务,并为我的模型上的CRUD和查询操作... 查看详情

Spring Boot - 实体中的自定义类字段

】SpringBoot-实体中的自定义类字段【英文标题】:SpringBoot-customclassfieldinentity【发布时间】:2017-11-2620:15:20【问题描述】:我有2个自定义类,OzBakim和GunlukEtkinlik。这些类不是实体。我需要在实体中使用这些类。但我得到一个错误... 查看详情

用于在 Spring Data Jpa 中从多个表中获取数据的自定义查询

】用于在SpringDataJpa中从多个表中获取数据的自定义查询【英文标题】:CustomQueryforfetchingdatafrommultipletablesinspringDataJpa【发布时间】:2017-08-1200:18:15【问题描述】:实体正在关注产品表@EntitypublicclassProductimplementsSerializable/*@Id@Generat... 查看详情

Springboot jpa:实体无法绑定不在表列中的自定义查询中的数据

】Springbootjpa:实体无法绑定不在表列中的自定义查询中的数据【英文标题】:Springbootjpa:Entitycan\'tbinddatafromcustomquerythat\'snotintablecolumn【发布时间】:2021-12-1310:51:22【问题描述】:我使用javaSpringbootJPA,mysql。我需要从我的自定义... 查看详情

带有spring-boot的自定义sql中的Liquibase参数

】带有spring-boot的自定义sql中的Liquibase参数【英文标题】:Liquibaseparametersincustomsqlwithspring-boot【发布时间】:2019-04-1910:16:51【问题描述】:我在jdk11上有一个spring-boot应用程序,使用maven,具有以下liquibase依赖项:<dependency><... 查看详情

Spring Boot JPA:如何查询表中的 JSON 列

】SpringBootJPA:如何查询表中的JSON列【英文标题】:SpringBootJPA:howdoqueryaJSONcolumninatable【发布时间】:2017-10-1013:19:57【问题描述】:我有一张表casemessage并有以下列。我正在尝试使用SpringFrameworkJPA搜索/查询JSON列..身份证created_bycreat... 查看详情

Spring boot - Application.properties 中的自定义变量

】Springboot-Application.properties中的自定义变量【英文标题】:Springboot-customvariablesinApplication.properties【发布时间】:2015-11-1014:05:59【问题描述】:我有一个使用restfulapi的springboot客户端。我可以使用application.properties中的任何密钥条... 查看详情

Spring Data JPA - 带有“@Param Date”的自定义@Query 不起作用

】SpringDataJPA-带有“@ParamDate”的自定义@Query不起作用【英文标题】:SpringDataJPA-custom@Querywith"@ParamDate"doesn\'twork【发布时间】:2017-03-2912:46:20【问题描述】:我创建了自定义查询以按日期获取发票数据,但它返回null。我确... 查看详情

Spring Boot,MongoDB,Pageable,按对象中的自定义方法排序

】SpringBoot,MongoDB,Pageable,按对象中的自定义方法排序【英文标题】:SpringBoot,MongoDB,Pageable,sortbyacustommethodintheobject【发布时间】:2020-04-2214:27:46【问题描述】:比如说我有以下设置,这样的模型:publicclassPost@IdprivateStringid;privat... 查看详情

spring boot 应用程序:jpa 查询返回旧数据

】springboot应用程序:jpa查询返回旧数据【英文标题】:springbootapplication:jpaqueryreturningolddata【发布时间】:2016-12-1918:44:14【问题描述】:我们创建了一个使用1.3.5版本的springboot项目。我们的应用程序与Mysql数据库交互。我们创建... 查看详情

Spring Boot中的JPA @query中没有传递参数[重复]

】SpringBoot中的JPA@query中没有传递参数[重复]【英文标题】:ParametersarenotbeingpassedintheJPA@queryinspringboot[duplicate]【发布时间】:2019-03-0816:11:53【问题描述】:我正在尝试在SpringBoot中通过JPA备份现有表。为此,我在下面的查询中将表... 查看详情

无法从 Spring Boot 应用程序中的自定义 yml 文件加载配置

】无法从SpringBoot应用程序中的自定义yml文件加载配置【英文标题】:Cannotloadconfigfromcustomymlfileinspringbootapplication【发布时间】:2017-11-1608:30:55【问题描述】:我正在从我的SpringBoot服务中的application.yml加载自定义配置。我已经通... 查看详情

@RepositoryRestResource 中的自定义实现

...1【问题描述】:我正在开发一个使用@RepositoryRestResource的spring-boot项目。有2个实体,Product和Domain,它们具有多对多关系。我想实现一个运行复杂查询的自定义REST调用。为了实现 查看详情