将列表传递给javax.persistent.query对象时发生异常

时间:2014-05-29 13:44:13

标签: sql oracle hibernate jpa

我有查询

"SELECT I.INDIVIDUAL_CASE_ID,D.ASSISTER_ID,I.FIRSTNAME,I.LASTNAME,D.LAST_UPDATE_TIMESTAMP,I.ELIGIBILITY_STATUS,I.INDIVIDUAL_CASE_ID," +
               "I.NUMBEROFHOUSEHOLDMEMBERS,I.HOUSEHOLD_INCOME FROM  EXTERNAL_INDIVIDUAL I, EE_DESIGNATE_ASSISTERS D " +
               "WHERE I.INDIVIDUAL_CASE_ID = D.INDIVIDUAL_ID AND D.ASSISTER_ID=:assisterId AND D.INDIVIDUAL_ID IN (:individualIds)";

我传递的参数如

query = em.createNativeQuery(queryStr);
query.setParameter("assisterId", assisterId);
query.setParameter("individualIds",indvIdList);

但是我的异常是

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ghix-entity] threw exception [Request processing failed; nested exception is
java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [individualIds]] with root cause
org.hibernate.QueryParameterException: could not locate named parameter [individualIds]
    at org.hibernate.engine.query.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:101)

3 个答案:

答案 0 :(得分:1)

本机查询无法识别命名参数。请看下面的链接似乎是类似的问题:JPA/Hibernate Native Queries do not recognize Parameters

尝试: "SELECT I.INDIVIDUAL_CASE_ID,D.ASSISTER_ID,I.FIRSTNAME,I.LASTNAME,D.LAST_UPDATE_TIMESTAM‌​P,I.ELIGIBILITY_STATUS,I.INDIVIDUAL_CASE_ID," + "I.NUMBEROFHOUSEHOLDMEMBERS,I.HOUSEHOLD_INCOME FROM EXTERNAL_INDIVIDUAL I, EE_DESIGNATE_ASSISTERS D " + "WHERE I.INDIVIDUAL_CASE_ID = D.INDIVIDUAL_ID AND D.ASSISTER_ID=?1 AND D.INDIVIDUAL_ID IN (?2)";

query = em.createNativeQuery(queryStr); 
query.setParameter(1, assisterId); 
query.setParameter(2,indvIdList);

答案 1 :(得分:0)

如果你使用Hibernate而不是其他JPA实现:

您可以从entityManager获取Hibernate会话:

Session session = entityManager.unwrap(Session.class);

然后you can use named parameters

List result = 
    session.createSQLQuery(
            "SELECT I.INDIVIDUAL_CASE_ID,D.ASSISTER_ID,I.FIRSTNAME,I.LASTNAME,D.LAST_UPDATE_TIMESTAMP,I.ELIGIBILITY_STATUS,I.INDIVIDUAL_CASE_ID," +
                    "I.NUMBEROFHOUSEHOLDMEMBERS,I.HOUSEHOLD_INCOME FROM  EXTERNAL_INDIVIDUAL I, EE_DESIGNATE_ASSISTERS D " +
                    "WHERE I.INDIVIDUAL_CASE_ID = D.INDIVIDUAL_ID AND D.ASSISTER_ID=:assisterId AND D.INDIVIDUAL_ID IN (:individualIds)"
    )
    .setLong("assisterId", assisterId)
    .setParameterList("individualIds", indvIdList)
    .list();

答案 2 :(得分:0)

这是我的问题。我错误地在查询对象中设置了一些额外的参数。删除后,列表工作正常。谢谢你的所有建议