简体   繁体   中英

insert the list parameter into the nativeQuery

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"] eg ["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();
    }



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(","));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM