skip to Main Content

I have a query like this that works well :

"SELECT * FROM elements we WHERE jsonb_exists_all(CAST(we.elements AS jsonb), ARRAY['TEST'])"

I need to use the .setParameter method from EntityManager to insert a whole array instead of ["CHEST"] e.g. ["TEST", "TEST1", "TEST2"]. 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 elements we WHERE jsonb_exists_all(CAST(we.elements AS jsonb), ARRAY[:array])";

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


        return query
                .setParameter("array", "TEST'")
                .setParameter("array", "TEST")
                .getResultList();
    }



2

Answers


  1. Chosen as BEST ANSWER

    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 ''


  2. 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(","));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search