Pregunta

I'd like to search a requested value in my database using the entity manager createquery function. Whenever I executed my method, I got the following error message:

llegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter searchvalue with expected type of int from query string

It says that a int value is required, but I think that every like expression requires a string value?

Entity:

public class Contract implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Basic(optional = false)
    @NotNull
    @Column(name = "number")
    private int number;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "type")
    private String type;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "description")
    private String description;

    @Basic(optional = false)
    @NotNull
    @Column(name = "start_date")
    @Temporal(TemporalType.DATE)
    private Date startDate;

    @Basic(optional = false)
    @NotNull
    @Column(name = "minimum_term")
    private int minimumTerm;

    @NotNull
    @Column(name = "cancellation_period")
    private int cancelTime;

    @Basic(optional = false)
    @NotNull
    @Column(name = "status")
    private int status;

    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Users userId;

    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Supplier supplierId;

    [...]
}

QueryString:

List mylist = em.createQuery("SELECT c FROM Contract c WHERE c.userId = :userid AND (c.number LIKE :search OR c.type LIKE :search OR c.description LIKE :search OR c.startDate LIKE :search OR c.supplierId.name LIKE :search OR c.cancelTime LIKE :search OR c.minimumTerm LIKE :search)", Contract.class)
    .setParameter("userid", user).setParameter("search", "%"+search+"%")
    .getResultList()

Thanks in advance! :)

¿Fue útil?

Solución

As @Andrei Nicusan stated you cannot us LIKE operator on an int.

You can cast the value to string and use in the where clause :

  1. Use a native query and use MySQL cast function.

  2. Use your JPA provider for extension function, if your using Hibernate you can use also use CAST (type as string). See here.

Otros consejos

First of all, for your number column, it is very normal to expect an integer as parameter because this is an integer-typed column. You can't compare or match numerical values with strings.

Second, LIKE clauses can only be applied to strings. See here.

To convert from int to string in JPQL you can use CAST

HERE are an example (NOTE: Assuming MyEntity has id with type int)

public List<MyEntity> getMyEntities(String idStr) {
   return entityManager.createQuery("SELECT m FROM MyEntity m WHERE CAST( m.id AS string ) LIKE :idStr")
         .setParameter("idStr", "%" + idStr+ "%").getResultList();
}

I am using JPA 2.1 with Hibernate provider.

I tested it and this previous code working fine with me, so i hope it can help some one :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top