在JPQL查询中使用数组

时间:2015-07-23 07:23:36

标签: java hibernate jpa jpql

如何在JPQL查询中使用数组列表?我想要这样的东西: 我正在通过这个

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

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);
}

目前,错误是

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

1 个答案:

答案 0 :(得分:14)

阅读this site似乎有可能,但您的数组必须是Collection,可能是这样的:

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));
}

应该帮助你。