Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Named Query

I wrote a named query in the entity class Voter

NamedQuery(name = "Voter.findvoter", query = "SELECT count(*) FROM Voter v WHERE v.voterID = :voterID" and where v.password= : password),

I want to call this named query and I also need to set voterID and password.

Can you help me. Thank you

like image 598
sandeep Avatar asked Apr 30 '10 09:04

sandeep


People also ask

How do you call a named query in the ignition?

Once you select a Named Query type, enter a name, and press 'create', and the specific named query template will open. The Select Query and the Update Query will have some sample parameters and queries to help you get started.

How do you define a named query?

A named query is a predefined query that you create and associate with a container-managed entity (see "Using Annotations"). At deployment time, OC4J stores named queries on the EntityManager . At run time, you can use the EntityManager to acquire, configure, and execute a named query.

Which annotation is correct for named query?

Hibernate Named Query by annotation@NameQuery annotation is used to define the single named query. Let's see the example of using the named queries: @NamedQueries(

Where do we use named queries?

Hibernate Named Query XMLquery element is used for HQL named queries and sql-query element is used for native sql named queries. We can use return element for declaring the entity to which resultset will be mapped. return-join is used when we have join of multiple tables.


2 Answers

I assume you've missed the @ symbol on your NamedQuery annotation?

In the code, you'd call it like this:

List results = em.createNamedQuery("Voter.findvoter")
    .setParameter("voterID", "blah")
    .setParameter("password","blahblahblah")
    .getResultList();
like image 90
Dick Chesterwood Avatar answered Oct 03 '22 23:10

Dick Chesterwood


There are two obvious issues with your named query that would cause a problems:

  • It is an annotation so it should be @NamedQuery not just NamedQuery
  • Your query is currently:

query = "SELECT count(*) FROM Voter v WHERE v.voterID = :voterID" and where v.password= : password.

The problem is that you terminate your String after :voterID, instead of after :password and you have "where" twice and you have a space between ":" and "password". Your query should look like this:

query = "SELECT count(*) FROM Voter v WHERE v.voterID = :voterID and v.password= :password"

(I have just moved the " to the end and removed the second "where" and the space after the ":")

like image 22
brent777 Avatar answered Oct 03 '22 23:10

brent777