1

I am using a query builder to create a query statement in hibernate. i need to pass a arrayList for in statement. How can this be done. Dummy code :

List<String> xyz = new ArrayList<String>("sam","tam","vam");
StringBuilder queryBuilder = new StringBuilder("select abc from tem where xyz in :xyzList");
Query query = entityManager.createNativeQuery(queryBuilder.toString());
query.setParameter("xyzList", xyz);
query.getResultList();

this is not working. it throws exceptions. Can somebody point me how to do this.

user2696466
  • 650
  • 1
  • 14
  • 33

2 Answers2

0

Use setParameterList("xyzList", new String []{"a","b","c"});

So if you have the list with you, you can do list.toArray() in place of new String []{"a","b","c"}

shikjohari
  • 2,278
  • 11
  • 23
  • `setParameterList()` is not available in `javax.persistence.Query`, which the OP is using since the query is created with `entityManager.createNativeQuery()`. That is a method of `org.hibernateQuery`. – Predrag Maric Feb 27 '15 at 10:12
0

You have seemed to be mistaken an entityManager.createNativeQuery with entityManager.createNamedQuery or entityManager.createQuery

The syntax of your query is JPQL, but you compile it as a native query syntax

What should work for you is to move from createNativeQuery simply to createQuery, or align it the other way around so write a proper native query if that is your goal.

Note just that if you're intent was to go for a native query you should stay away from the named parameters. In your case it would work since you're using hibernate as persistence provider, but otherwise, named parameters in native queries are not supported according to JPA specification

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Master Slave
  • 27,771
  • 4
  • 57
  • 55