Home > Software design >  insert the list parameter into the nativeQuery
insert the list parameter into the nativeQuery

Time:12-23

I have a query like this that works well :

"SELECT * FROM worklist_elements we WHERE jsonb_exists_all(CAST(we.body_parts AS jsonb), ARRAY['CHEST'])"

I need to use the .setParameter method from EntityManager to insert a whole array instead of ["CHEST"] e.g. ["CHEST", "CHEST1", "CHEST2"]. It seems to me that .setParameter converts the argument into a string. So far I have only found a workaround for assigning 2 values to the same key:

String queryFromFilter = "SELECT * FROM worklist_elements we WHERE jsonb_exists_all(CAST(we.body_parts AS jsonb), ARRAY[:array])";

        Query query = entityManager.createNativeQuery(queryFromFilter, Worklist.class);


        return query
                .setParameter("array", "CHEST'")
                .setParameter("array", "K.piersiowa")
                .getResultList();
    }



CodePudding user response:

This question seems related to this one where it's suggested to convert the list of Strings into a single-quoted comma-separated String and then passing the String onto JPA Query.

For instance:

    var params = Arrays.asList("param1", "param2", "param3");
    var queryParam = params.stream().map(p -> "'"   p   "'").collect(Collectors.joining(","));

CodePudding user response:

rather the problem here is that the value from the .setParameters method puts the parameter in '' e.g. 'exampleParameter' and I need it not to be ''

  • Related