简体   繁体   English

JPA 本机查询中未设置参数 Spring Boot

[英]Parameter not set in JPA Native Query in Spring Boot

i am developing a backend in Spring Boot, i am using JPA for all the DB operations.我正在 Spring 引导中开发后端,我正在使用 JPA 进行所有数据库操作。 The problem is the following:问题如下:

i receive a parameter named "responsableServicio" from the frontend, first it gets to the controller method where i checked the parameter gets there correctly.我从前端收到一个名为“responsableServicio”的参数,首先它进入 controller 方法,在该方法中我检查了参数是否正确到达那里。 However, when the parameter goes from controller to repository, the value is not getting correctly, it seems being empty because my query should return 1 record and is returning 0 records.但是,当参数从 controller 转到存储库时,该值未正确获取,它似乎为空,因为我的查询应返回 1 条记录并返回 0 条记录。

i would like to highlight that if i put the parameter hardcoded in the query, it returns the correct result 1.我想强调的是,如果我将参数硬编码在查询中,它会返回正确的结果 1。

i followed some examples but is not working yet.我遵循了一些示例,但还没有工作。 What am i doing wrong?我究竟做错了什么? Thanks in advance提前致谢

Formulario Controller方程式 Controller

@PostMapping(path = "/getCountAnexos")
    @ResponseBody
    public String countAnexosRechazo(@RequestBody String responsableServicio) {
        Map<String, Object> json = new HashMap<String, Object>();
        // Cambiar a nativeQuery

        System.out.println("*******************************************");
        System.out.println("*******************************************");
        System.out.println(responsableServicio);
        System.out.println("*******************************************");
        System.out.println("*******************************************");
        String respuesta = formularioRepository.getAnexosRechazados(responsableServicio);
        System.out.printf("RESPUESTA",respuesta);

        return respuesta;
}

Formulario Repository公式库

// Consulta para obtener el numero de Anexos Rechazados de acuerdo el Usuario
    @Query(value = "SELECT COUNT(*) FROM formulario where year(fechaTramite)='2020' and responsableServicio = :responsableServicio and (estatusGestorContratos = 'Rechazado' or estatusAFTI = 'Rechazado')", nativeQuery = true)
    public abstract String getAnexosRechazados(@Param("responsableServicio") String responsableServicio);

What i get from console我从控制台得到什么

img图像

The problem is in Controller Class.问题出在 Controller Class 中。 It is taking String as complete JSON with brackets.它将字符串作为带有括号的完整 JSON 。 Here you have taken a @PostMapping and using @RequestBody String responsableServicio as a method parameter.在这里,您采用了 @PostMapping 并使用 @RequestBody String responsableServicio 作为方法参数。 You cannot take String as a type parameter, you have to create a class having String responsableServicio as a variable with its getters/setters.您不能将 String 作为类型参数,您必须创建一个 class ,将 String responsableServicio 作为带有 getter/setter 的变量。 Then within controller take argument as a class paramater.然后在 controller 中将参数作为 class 参数。

class RequestDTO{
    String responsableServicio;
    public String getResponsableServicio() {
        return responsableServicio;
    }
    public void setResponsableServicio(String responsableServicio) {
        this.responsableServicio = responsableServicio;
    }
} 

Write controller as将 controller 写为

@PostMapping(path = "/getCountAnexos")
@ResponseBody
public String countAnexosRechazo(@RequestBody RequestDTO responsableServicio) {
    Map<String, Object> json = new HashMap<String, Object>();
    // Cambiar a nativeQuery

    System.out.println("*******************************************");
    System.out.println("*******************************************");
    System.out.println(responsableServicio.getResponsableServicio());
    System.out.println("*******************************************");
    System.out.println("*******************************************");
    String respuesta = formularioRepository.getAnexosRechazados(responsableServicio.getResponsableServicio());
    System.out.printf("RESPUESTA",respuesta);

    return respuesta;
}

That will work fine..!!那会很好用.. !!

If that too not works then, 1) Check the @Query you have imported should be from org.springframework.data.jpa.repository.Query如果那也不起作用,1)检查您导入的@Query 应该来自org.springframework.data.jpa.repository.Query

2) Check the Repository interface has @Repository annotation. 2) 检查Repository 接口是否有@Repository 注解。

3) Check the @Param is imported from org.springframework.data.repository.query.Param 3) 检查@Param 是从org.springframework.data.repository.query.Param导入的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM