Help with JPQL to make a kind of filter with LIKE JAVA

0

I have a problem. I want to make a JPQL , and make a kind of filter that lists me the type of merchandise that have similarity with the name specified by the user.

@Override
public List<TblStock> findByName(String nombre) {
    TypedQuery<TblStock> q = getEntityManager().createQuery("SELECT p FROM TblStock p where p.tipoMercancia like '%:nombre%' ", TblStock.class);
    q.setParameter("nombre", tipoMercancia);
    return q.getResultList();    
}

When making this query, I get the following error:

  

java.lang.IllegalArgumentException: You have attempted to set a parameter value using a name of typeMercance that does not exist in   the query string SELECT p FROM TblStock p where p.typeMercance like   '%: typeMercance%'.

Clearly you are not accepting the nombre parameter, but I do not know how else you could do a query with LiKE .

I appreciate any contribution, thank you very much.

    
asked by Oscar David 14.07.2018 в 00:47
source

1 answer

2

According to the answer:

  

link

It seems that your problem is the like that you put it from the query , so the value remains incomplete, the wait a key called '%:tipoMercancia%' instead of tipoMercancia .

So the solution with the correction you mentioned in the comment would be:

@Override
public List<TblStock> findByName(String nombre) {
    TypedQuery<TblStock> q = getEntityManager().createQuery("SELECT p FROM TblStock p where p.tipoMercancia like :nombre ", TblStock.class);
    q.setParameter("nombre", "%"+nombre+"%");
    return q.getResultList();    
}
    
answered by 14.07.2018 / 01:08
source