Select where string contains substring using Panache

2.3k views Asked by At

I'm currently trying to implement the function "Load all X where X.name contains y" using a Ms SQL-Server and the Panache-Repositories. I know the SQL-Query "SELECT * FROM X WHERE X.name LIKE '%0y%'" works, but I can't get it to work using the Panache-Query. I've tried

@ApplicationScoped
public class XRepository implements PanacheRepository<X> {

  public List<X> listWhereLike(String like) {
    return list("name like ?1", "%" + like + "%");
  }

  public List<X> listWhereLike(String like) {
    return list("name like %?1%", like);
  }

  public List<X> listWhereLike(String like) {
    return list("contains(name, ?1)", like);
  }
}

but none of them work. They either throw an exception because of the unexpected character '%' or '(' or they simply return an empty list.

What am I missing?

1

There are 1 answers

0
Malek Zarkouna On

This is the solution , you just need to put the pattern in a String before adding it to the query;

  public List<X> listWhereLike(String like) {

    String searchInput = "%" + like + "%";

    return list("name like ?1", searchInput );
  }