简体   繁体   中英

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]

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.

You need to use 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 .

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

}

(by the way, sorry for my previous (deleted) answer. It was not pertinent, and wrong).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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