Home > Mobile >  java.lang.IllegalArgumentException: Parameter value [%1%] did not match expected type [java.lang.Int
java.lang.IllegalArgumentException: Parameter value [%1%] did not match expected type [java.lang.Int

Time:12-05

Friends, I have this problem and I can't find the solution, any suggestions?

@Repository(value = "TblagendalabRepository")
public interface TblagendalabRepository extends JpaRepository<Tblagendalab, Integer>{
    
     @Query(value = "SELECT p FROM Tblagendalab p WHERE p.tblagendalabPK.idExamen LIKE %:q%")
      List<Tblagendalab> findByExamenLab(@Param("q") Integer q);

}

Controller

@GetMapping(value = "/buscar")
    public String buscarExamen(Model model, @RequestParam(value = "buscar", required = false)Integer q, @RequestParam(value="idExamen", required = false) Integer idExamen) {

                        List<Tblagendalab> examenes = this.tblagendalabRepository.findByExamenLab(q);
            model.addAttribute("examenes", examenes);
            return "/views/agenda/agendar";

    }

Entities

@Entity
@Table(name = "tblagendalab")
public class Tblagendalab implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected TblagendalabPK tblagendalabPK;
    @JoinColumn(name = "id_examen", referencedColumnName = "id_examen", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Tblexamen tblexamen;
    @JoinColumn(name = "idhora", referencedColumnName = "idhora", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Tblhora tblhora;
    @JoinColumn(name = "id_lab", referencedColumnName = "id_lab", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Tbllab tbllab;
    @OneToMany(mappedBy = "tblagendalab")
    private List<Tblreserva> tblreservaList;

Entities PK

@Embeddable
public class TblagendalabPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "id_examen")
    private int idExamen;
    @Basic(optional = false)
    @Column(name = "idhora")
    private int idhora;
    @Basic(optional = false)
    @Column(name = "id_lab")
    private int idLab;

Output

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

I tried the solutions here on Stack Overflow, but they didn't work. I modified the @Param to String, int, the query directly from the Tblagendalab entitie etc.

Debug

enter image description here

CodePudding user response:

You're trying to compare an integer value with a string. no matter how you change the input @Params to any type, the problem is %:q% can not be used to compare for an integer. If you want to do the comparative that way, I think you must cast the value to a string before compare

@Query(value = "SELECT p FROM Tblagendalab p WHERE cast(p.tblagendalabPK.idExamen as string) LIKE '%:q%'")
List<Tblagendalab> findByExamenLab(@Param("q") Integer q);

CodePudding user response:

It's all about %%;
When use jpa query sql, u must concat % with the parameter
The right sql would be:

@Query(value = "SELECT p FROM Tblagendalab p WHERE p.tblagendalabPK.idExamen LIKE CONCAT('%',:q,'%')" , nativeQuery = true );
  • Related