User
User

Reputation: 603

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?

Upvotes: 0

Views: 5180

Answers (2)

NoDataFound
NoDataFound

Reputation: 11959

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).

Upvotes: 0

Marcin Szawurski
Marcin Szawurski

Reputation: 1333

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")

Upvotes: 1

Related Questions