【问题标题】:Hibernate SQLQuery with condition in list [duplicate]使用列表中的条件休眠 SQLQuery [重复]
【发布时间】:2012-04-01 00:43:59
【问题描述】:

我正在尝试将整数列表传递给 SQLQuery。但是它会抛出一个错误说"Exception : could not locate named parameter [ids]; nested exception is org.hibernate.QueryParameterException: could not locate named parameter [ids]"

这就是我的查询的样子:

List<Integer> ids = new ArrayList<Integer>(5);
//Fill something in ids
return session.createSQLQuery("select igf.foo_id from group_feed igf where igf.id in (:ids)")
    .setMaxResults(pageSize)
    .setParameterList("ids", ids)
    .setResultTransformer(Transformers.aliasToBean(GroupFeed.class))
    .list();

我做错了什么?我不允许使用 setParameterListHibernate SQLQuery 吗?我无法从 jBoss 中的 Hibernate 文档中了解多少。

【问题讨论】:

    标签: mysql sql spring hibernate


    【解决方案1】:
    List<Integer> ids = new ArrayList<Integer>(5);
    

    生成一个具有 5 个插槽容量的空列表:

    ids {null, null, null, null, null}
    
    for (Integer i : ids) {
                System.out.println("" + i);
    }
    

    不会打印任何东西。列表为空。

    您确定列表中至少包含一项吗?

    Hibernate查找参数的方式我认为你必须:

    return session.createSQLQuery("select igf.foo_id from group_feed igf where igf.id in ( :ids )")
    .setMaxResults(pageSize)
    .setParameterList("ids", ids)
    .setResultTransformer(Transformers.aliasToBean(GroupFeed.class))
    .list();
    

    我认为Hibernate 不知道:ids,因为它是(:ids)

    【讨论】:

    • 还是一样。异常说:“异常:找不到命名参数[ids];” .我想这是指没有找到“ids”数据的事实。
    • 我不想把初始化代码放在这里,所以我放了这个注释-> //Fill something in ids.我猜@Vikram 说的是正确的。
    • 我知道你不想:)
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多