【问题标题】:using array in JPQL query在 JPQL 查询中使用数组
【发布时间】:2015-10-13 08:46:03
【问题描述】:

如何在 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)]

【问题讨论】:

    标签: java hibernate jpa jpql


    【解决方案1】:

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

    应该能帮到你。

    【讨论】:

    • 正是我需要的。谢谢
    猜你喜欢
    • 2012-01-17
    • 2019-06-24
    • 2019-09-20
    • 1970-01-01
    • 2014-09-28
    • 2014-07-23
    • 2014-08-01
    • 2015-08-05
    • 1970-01-01
    相关资源
    最近更新 更多