简体   繁体   English

找不到命名参数

[英]Could not locate named parameter

I have the following query: 我有以下查询:

<named-native-query name="GET_Objects_REPORT">
    <query>
        <![CDATA[
               SELECT *
               FROM KAP.VC
               JOIN KAP.V ON VC.ID = V.ID
               JOIN KAP.VI ON VC.ID = VI.ID AND (VI."DATETIME" BETWEEN :startDate and :endDate)
         ]]>
    </query>
</named-native-query>               

While executing the query, I get the following exception: 执行查询时,出现以下异常:

java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [startDate] java.lang.IllegalArgumentException:org.hibernate.QueryParameterException:找不到命名参数[startDate]

public List<Object[]> getAllObjects(final Date startDate, final Date endDate) {
    final Query q = em.createNativeQuery("GET_Objects_REPORT");
    q.setParameter("startDate", startDate);
    q.setParameter("endDate", endDate);

    return q.getResultList();
}

Could you please advice what is the problem of my query? 您能告诉我我的查询是什么问题吗?

Your SQL query is GET_Objects_REPORT (which is not valid SQL), there is no startDate parameter. 您的SQL查询是GET_Objects_REPORT (无效的SQL),没有startDate参数。

You need to use em.createNamedQuery("GET_Objects_REPORT") 您需要使用em.createNamedQuery("GET_Objects_REPORT")

That won't answer the question as to why the query is not found while in a XML file (orm.xml ? is it valid according to its schema ?), but you can put your named queries, native or not, in the entity they belong to (this is not a requirement) instead of orm.xml or persistence.xml . 这不会回答以下问题:为什么在XML文件中找不到查询(orm.xml?根据其模式是否有效?),但是您可以将命名查询(无论是否是本机)放入实体中它们属于(不是orm.xml )而不是orm.xmlpersistence.xml

@Entity
@NamedNativeQueries({
@NamedNativeQuery(name="Foobar", query = "...")
})
public class MyEntity {

}

(by the way, sorry for my previous (deleted) answer. It was not pertinent, and wrong). (顺便说一句,对不起我以前的(已删除)答案。这没有针对性,而且是错误的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM