DharmasriS DharmasriS - 1 year ago 74
SQL Question

How to match individual substring to a String in Sql?

I am Calling a named query using Entity Manager in my main class as below

this.list = em.createNamedQuery(MyClass.Check_Name).setParameter("name", NAMES).getResultList();

And the Named query is as below

@NamedQuery(name = MyClass.Check_Name, query = "SELECT sc FROM sampleTable sc where sc.name IN :name")})

I'm passing NAMES containing the String value ('A','B','C')

now the problem I'm facing is the sc.name column in the table consists of individual values as "A", "B", "C" but I'm comparing the column sc.name as whole in the query to NAMES variable.Hence it is returning ) result list.

How to match individual substring to a String?

Answer Source

pass a List (or any Collection) to setParameter method.


hibernate specially does an instance of check for collection

 public TypedQuery<X> setParameter(String name, Object value) {
    try {
      if(value instanceof Collection) {
        this.query.setParameterList(name, (Collection)value);
      } else {
        this.query.setParameter(name, value);

      this.registerParameterBinding(this.getParameter(name), value);
      return this;
    } catch (QueryParameterException var4) {
      throw new IllegalArgumentException(var4);
    } catch (HibernateException var5) {
      throw this.getEntityManager().convert(var5);

other JPA implementations might have the same kind of behaviour

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download