Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)]
like image 576
rakamakafo Avatar asked Jul 23 '15 07:07

rakamakafo


People also ask

Can we use subquery in JPQL?

A subselect is a query embedded into another query. It's a powerful feature you probably know from SQL. Unfortunately, JPQL supports it only in the WHERE clause and not in the SELECT or FROM clause. Subqueries can return one or multiple records and can use the aliases defined in the outer query.

Why JPQL is better than SQL in Web application?

JPA allows you to avoid writing DML in the database specific dialect of SQL. JPA allows you to load and save Java objects and graphs without any DML language at all. When you do need to perform queries JPQL allows you to express the queries in terms of the Java entities rather than the (native) SQL tables and columns.

Can we use inner join in JPQL?

In JPQL, JOIN can only appear in a FROM clause. The INNER keyword is optional (i.e. INNER JOIN is equivalent to JOIN).


1 Answers

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.

like image 104
Alex Sifuentes Avatar answered Oct 05 '22 20:10

Alex Sifuentes