简体   繁体   中英

using array in JPQL query

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)]

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.

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