Search code examples
jpajpa-2.0openjpa

JPA In Query as list of string not giving answer


List<Thing> results = em.createQuery(
"select t from Thing t where t.status in ('A', 'E')")
.getResultList();

This is using create query and without parameter.And it works fine

List<String> statusList= new ArrayList<String>();
statusList.add("A");
statusList.add("E");

 List<Thing> results = em.createQuery(
"select t from Thing t where t.status in ('"+statusList.toSting()+"')")
.getResultList();

This is not working .I don't want IN(:parameter) to set .Only I need in the above descried one


Solution

  • You can not do it like above You have to use in as below

    Query query=em.createQuery(
    "select t from Thing t where t.status in (:list)")
    query.setParameterList("list", statusList)
    query.getResultList();
    

    Your statusList.toSting() will not produce a string like 'A','E' becuase you have added the statusList.toSting() in () so if your ultimate result will looks like ('A','E') then only your query will give correct results