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
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.
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.
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(
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.
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();
There are two obvious issues with your named query that would cause a problems:
@NamedQuery
not just NamedQuery
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 ":")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With