Friday, February 12, 2010

Programmer - Best way to write a named SQL query that returns if row exists?

So I have this SQL query,



<named-query name="NQ::job_exists">
<query>
select 0 from dual where exists (select * from job_queue);
</query>
</named-query>


Which I plan to use like this:



Query q = em.createNamedQuery("NQ::job_exists");
List<Integer> results = q.getResultList();
boolean exists = !results.isEmpty();
return exists;


I am not very strong in SQL/JPA however, and was wondering whether there is a better way of doing it (or ways to improve it). Should I for example, write (select jq.id from job_queue jq) instead of using a star??

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails