参数值 [%Gabrek%] 与预期类型 [java.lang.Character (n/a)] 不匹配;

发布于 2025-01-17 06:47:04 字数 6508 浏览 0 评论 0原文

我一直在使用 JPA 在 Spring Boot Web 中编写一个程序,并且我正在使用查询来访问带有“contains”和“ignorecase”过滤器的一些数据,我之前在其他程序中已经这样做过,并且运行良好,但现在我遇到了这个错误,我完全迷失了,因为我在谷歌中找不到任何东西,我真的很深入地寻找它为什么会发生,到目前为止我还没有看到我的任何不合适的地方代码中,声明的变量类型似乎没问题,但正如我所说,我迷路了。值得一提的是,由于某种原因,当我第一次在我的网站上执行查询时,一切正常,我得到了正确的结果等,但是当我回到家并尝试另一个查询(甚至相同的查询)时,我会得到正确的结果。 )我收到错误。代码如下:

模型

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Serie {
    
    @Id
    @Column(columnDefinition = "NUMERIC(19,0)")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String title;
    private String red;
    @Column(columnDefinition = "NUMERIC(19,0)")
    private double rating;

存储库

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import cl.desafiolatam.imdb.modelo.Serie;

public interface SerieRepository extends JpaRepository<Serie, Integer> {

    public List<Serie> findByTitleContainingIgnoreCase(String title);
    
}

服务

import cl.desafiolatam.imdb.vo.SerieVO;

public interface SerieService {
    
    public SerieVO findByTitleContainingIgnoreCase(String title);

}

服务实现

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cl.desafiolatam.imdb.dao.SerieRepository;
import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;

@Service
public class SerieServiceImpl implements SerieService {
    
    private static final Logger logger = LoggerFactory.getLogger(SerieServiceImpl.class);
    
    @Autowired
    SerieRepository dao;
    SerieVO respuesta;

    @Override
    @Transactional(readOnly = true)
    public SerieVO findByTitleContainingIgnoreCase(String title) {
        
        respuesta = new SerieVO("Ha ocurrido un error!", "104", new ArrayList<Serie>());

        try {
            List<Serie> serie = dao.findByTitleContainingIgnoreCase(title);
            System.out.println(serie);
            if(serie.size() > 0) {
                respuesta.setSeries(serie);
                respuesta.setMensaje("Se ha encontrado el registro");
                respuesta.setCodigo("0");
            } else {
                respuesta.setMensaje("No se ha encontrado el registro");
                respuesta.setCodigo("104");
            }
        } catch (Exception e) {
            logger.error("Error al buscar la serie", e);
        }
        
        return respuesta;
    }

}

可视化对象

import java.util.List;

import cl.desafiolatam.imdb.modelo.Serie;

public class SerieVO extends GenericVO {
    
    List<Serie> series;

    public SerieVO(String mensaje, String codigo, List<Serie> series) {
        super(mensaje, codigo);
        this.series = series;
    }

    public SerieVO() {
        super();
    }

控制器< /strong>

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;

@Controller
public class SerieController {

    private final static Logger logger = LoggerFactory.getLogger(SerieController.class);

    @Autowired
    private SerieService svc;

@GetMapping("/buscarSerie")
    public ModelAndView buscarSerie(Model model, @RequestParam String nombreSerie) {
        
        SerieVO respuestaServicio = new SerieVO();
        respuestaServicio.setMensaje("No se ha encontrado la serie");
        
        try {
            respuestaServicio = svc.findByTitleContainingIgnoreCase(nombreSerie);
            model.addAttribute("listaSeries", respuestaServicio.getSeries());
            return new ModelAndView("resultadoserie");
        } catch (Exception e) {
            logger.error("Error al buscar la serie", e);
        }
        
        return new ModelAndView("redirect:/user");
        
    }
}

搜索输入

<div class="d-flex justify-content-center pb-2">
        <div class="container row">
            <div class="col-md-4">
                <div class="d-flex justify-content-center">
                    <h2>Buscar serie</h2>
                </div>
            </div>
            <div class="col-md-8">
                <form action="buscarSerie" method="get">
                    <div class="row g-2">
                        <div class="col-md">
                            <div class="form-floating">
                                <input type="text" class="form-control" id="floatingInputGrid"
                                    value="" name="nombreSerie" required> <label
                                    for="floatingInputGrid">Serie</label>
                            </div>
                        </div>
                    </div>
                    <div class="d-flex justify-content-center pt-4">
                        <input type="submit" class="btn m-2 btn-dark" value="Buscar" />
                    </div>
                </form>
            </div>
        </div>
    </div>

正如我所说,我真的迷路了,到处研究,并检查了我上一个项目中的代码,我只是不明白为什么这个让我这么肮脏。一开始就不会失败,它给了我一线希望,当我想重试时,它又粉碎了那一点点希望。 :)

我尝试删除我的代码并从我知道它按预期工作的项目中复制和粘贴,更改变量和参数。名称以使其适用于新程序,但不起作用。进行了并排比较,尝试使用 @Query 编写特定指令。寻找信息。仅使用“包含”过滤器,但没有任何效果。

i've been writing wirting a program in Spring Boot Web with JPA and i'm using a query to access some data with a 'contains' and 'ignorecase' filter, i've done this before in other programs and it has worked fine, but now i'm getting this error, i'm completely lost at this point since i can't find anything in google, i went really far down the rabbit hole looking as to why it happens and so far i don't see anything out of place in my code, the type of variable declared seems to be okay but as i've said, i'm lost. It's important to mention that for some reason when I do the query on my website for the first time, everything works fine, i get the proper results and all, but when I go back to home and try with another query (or even the same) i get the error. Code below:

Model

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Serie {
    
    @Id
    @Column(columnDefinition = "NUMERIC(19,0)")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String title;
    private String red;
    @Column(columnDefinition = "NUMERIC(19,0)")
    private double rating;

Repository

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import cl.desafiolatam.imdb.modelo.Serie;

public interface SerieRepository extends JpaRepository<Serie, Integer> {

    public List<Serie> findByTitleContainingIgnoreCase(String title);
    
}

Service

import cl.desafiolatam.imdb.vo.SerieVO;

public interface SerieService {
    
    public SerieVO findByTitleContainingIgnoreCase(String title);

}

Service implementation

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cl.desafiolatam.imdb.dao.SerieRepository;
import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;

@Service
public class SerieServiceImpl implements SerieService {
    
    private static final Logger logger = LoggerFactory.getLogger(SerieServiceImpl.class);
    
    @Autowired
    SerieRepository dao;
    SerieVO respuesta;

    @Override
    @Transactional(readOnly = true)
    public SerieVO findByTitleContainingIgnoreCase(String title) {
        
        respuesta = new SerieVO("Ha ocurrido un error!", "104", new ArrayList<Serie>());

        try {
            List<Serie> serie = dao.findByTitleContainingIgnoreCase(title);
            System.out.println(serie);
            if(serie.size() > 0) {
                respuesta.setSeries(serie);
                respuesta.setMensaje("Se ha encontrado el registro");
                respuesta.setCodigo("0");
            } else {
                respuesta.setMensaje("No se ha encontrado el registro");
                respuesta.setCodigo("104");
            }
        } catch (Exception e) {
            logger.error("Error al buscar la serie", e);
        }
        
        return respuesta;
    }

}

Visual object

import java.util.List;

import cl.desafiolatam.imdb.modelo.Serie;

public class SerieVO extends GenericVO {
    
    List<Serie> series;

    public SerieVO(String mensaje, String codigo, List<Serie> series) {
        super(mensaje, codigo);
        this.series = series;
    }

    public SerieVO() {
        super();
    }

Controller

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;

@Controller
public class SerieController {

    private final static Logger logger = LoggerFactory.getLogger(SerieController.class);

    @Autowired
    private SerieService svc;

@GetMapping("/buscarSerie")
    public ModelAndView buscarSerie(Model model, @RequestParam String nombreSerie) {
        
        SerieVO respuestaServicio = new SerieVO();
        respuestaServicio.setMensaje("No se ha encontrado la serie");
        
        try {
            respuestaServicio = svc.findByTitleContainingIgnoreCase(nombreSerie);
            model.addAttribute("listaSeries", respuestaServicio.getSeries());
            return new ModelAndView("resultadoserie");
        } catch (Exception e) {
            logger.error("Error al buscar la serie", e);
        }
        
        return new ModelAndView("redirect:/user");
        
    }
}

Search input

<div class="d-flex justify-content-center pb-2">
        <div class="container row">
            <div class="col-md-4">
                <div class="d-flex justify-content-center">
                    <h2>Buscar serie</h2>
                </div>
            </div>
            <div class="col-md-8">
                <form action="buscarSerie" method="get">
                    <div class="row g-2">
                        <div class="col-md">
                            <div class="form-floating">
                                <input type="text" class="form-control" id="floatingInputGrid"
                                    value="" name="nombreSerie" required> <label
                                    for="floatingInputGrid">Serie</label>
                            </div>
                        </div>
                    </div>
                    <div class="d-flex justify-content-center pt-4">
                        <input type="submit" class="btn m-2 btn-dark" value="Buscar" />
                    </div>
                </form>
            </div>
        </div>
    </div>

As i've said, im really lost, researched everywhere, and checked the code in my last projects, i just can't find out why this one does me this dirty. Won't even fail at the start, it gives me a glimpse of hope and when i want to retry it, it crushes that little hope. :)

I tried deleting my code and copy&paste from projects where i know it works as intended, changed the variable and param. names to make it work with the new program but didn't work. Did a side by side comparison, tried a @Query writing the specific instruction. Looking for info. only with the 'contains' filter and yet, nothing worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隐诗 2025-01-24 06:47:04

根据 Spring Data JPA issue #2472 这似乎是 Hibernate 5.6.6 和 5.6.7 中的一个问题。

Hibernate 错误为 HHH-15142

解决方案是降级到 Hibernate 5.6.5 或等待 Hibernate 补丁来解决此问题。

更新:根据上述错误报告,该问题已在 5.6.9 版本中解决。

According to the Spring Data JPA issue #2472 this seems to be a problem in Hibernate 5.6.6 and 5.6.7.

The Hibernate bug is HHH-15142.

The solution is to either downgrade to Hibernate 5.6.5 or wait for a Hibernate patch to solve this issue.

Update: According to the bug report above this is resolved in version 5.6.9.

最近可好 2025-01-24 06:47:04

我有同样的错误,我尝试像这样提取 JPQL 查询

@Query("select u from user u where upper(u.name) like upper(?1)")
List<User> findByNameLikeIgnoreCase(final String pName) ;

,它在我的情况下有效。

I had the same bug and I tried to Extract JPQL query like that

@Query("select u from user u where upper(u.name) like upper(?1)")
List<User> findByNameLikeIgnoreCase(final String pName) ;

and it's worked in my case.

瞎闹 2025-01-24 06:47:04

尝试使用 @Param 注释:

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import cl.desafiolatam.imdb.modelo.Serie;
import org.springframework.data.repository.query.Param;

public interface SerieRepository extends JpaRepository<Serie, Integer> {   
    public List<Serie> findByTitleContainingIgnoreCase(@Param(title) String title);
}

问题 的解决方案

Try using @Param annotation:

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import cl.desafiolatam.imdb.modelo.Serie;
import org.springframework.data.repository.query.Param;

public interface SerieRepository extends JpaRepository<Serie, Integer> {   
    public List<Serie> findByTitleContainingIgnoreCase(@Param(title) String title);
}

Solution from this issue

昔日梦未散 2025-01-24 06:47:04

我也遇到了同样的问题。首先,Web 应用程序最初会运行,并因此返回类型错误参数值 [%ben%] 与预期类型 [java.lang.Character (n/a)] 不匹配。将 pom.xml 中的 hibernate 版本降级到 5.6.5.Final 有帮助,现在运行良好

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.6.5.Final</version>
        </dependency>

I experienced the same problem too. First the web app would run initially and consequently return type errors Parameter value [%ben%] did not match expected type [java.lang.Character (n/a)];. Downgrading the hibernate verion in pom.xml to 5.6.5.Final helped and it works well now

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.6.5.Final</version>
        </dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文