简体   繁体   English

Spring数据包含不适用于整数

[英]Spring Data Containing doesn't work for Integer

I am implementing search method. 我正在实现搜索方法。 And i want to searach for integers. 我想搜索整数。 So i have this method in repo: 所以我在回购中有这种方法:

public interface CarRepository extends CrudRepository<Car, Long> {
      List<Car> findByRocznikIsContaining(Integer rocznik);
}

My Entity: 我的实体:

@Entity
    public class Car {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String model;
private @Lob
String historiaSamochodu;
private Double przebieg;
private Integer rocznik;

But when i pass to my method any integer eg: 但是当我将参数传递给我的方法时,例如:

findByRocznikIsContaining(5);

It throws an error: 它抛出一个错误:

java.lang.IllegalArgumentException: Parameter value [%5%] did not match expected type [java.lang.Integer (n/a)]

Any idea why? 知道为什么吗?

I've checked this link https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation and for findBy...Containing it shows the following pseudo SQL code … where x.firstname like ?1 (parameter bound wrapped in %) . 我已经检查了此链接https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation并为findBy...Containing它显示了后面的伪SQL代码… where x.firstname like ?1 (parameter bound wrapped in %) Which in your case is transformed in where x.roczik like %5% . 在您的情况下,哪个转换为where x.roczik like %5% And basically %5% is a String and cannot be converted to Integer. 基本上%5%是字符串,不能转换为整数。 You could try to go with @Query annotation for that method like this: 您可以尝试为该方法使用@Query注释,如下所示:

@Query("select c from Car c where to_char(c.rocznik) like %?1%")
List<Car> findByRocznikIsContaining(Integer rocznik);

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

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