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)]
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.
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.
In JPQL, JOIN can only appear in a FROM clause. The INNER keyword is optional (i.e. INNER JOIN is equivalent to JOIN).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With