How to call Named Query

sandeep picture sandeep · Apr 30, 2010 · Viewed 42.1k times · Source

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

Answer

Dick Chesterwood picture Dick Chesterwood · Apr 30, 2010

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();