11

How can I use an array list in JPQL query? I want something like this: I am passing this

private static final String[] MASKS = {"30109", "30111"};

into

public List<Account> findAccount(String[] Masks){
StringBuilder sb = new StringBuilder("from AccountTable a where SUBSTRING(a.Account,1,5) in :Masks ");
Query q = em.createQuery(sb.toString(), AccountTable.class)
                .setParameter("Masks",Masks);
}

currently, error is

Encountered array-valued parameter binding, but was expecting [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Encountered array-valued parameter binding, but was expecting [java.lang.String (n/a)]
0

1 Answer 1

24

Reading this site it seems that is possible, but your array must be a Collection, maybe something like this:

public List<Account> findAccount(String[] Masks){
    StringBuilder sb = new StringBuilder("from AccountTable a where SUBSTRING(a.Account,1,5) in :Masks ");
    Query q = em.createQuery(sb.toString(), AccountTable.class)
                .setParameter("Masks", Arrays.asList(MASKS));
}

Should help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I need. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.