I use Hibernate to retrieve the number of rows for a particular request. Let's say I have a table called "Man" with various columns. One of these columns is "name".
If I wanted to get the number of people named "Andrey", which of these methods would be the most effective? Assuming there is a performance difference between some / all of them. Is there a better way to do this using Hibernate / SQL?
(1) Select all columns
Query query = session.createQuery("from Person where name= :name"); query.setParameter("name", name); List result = query.list(); int count = result.size();
(2) Select only the name column
Query query = session.createQuery("select name from Person where name= :name"); query.setParameter("name", name); List result = query.list(); int count = result.size();
(3) Using Count in a Request
Query query = session.createQuery("select count(*) from Person where name= :name"); query.setParameter("name", name); long count = (Long) query.uniqueResult();
(4) Using Count with a name column in a query
Query query = session.createQuery("select count(name) from Person where name= :name"); query.setParameter("name", name); long count = (Long) query.uniqueResult();
Edit: Sorry, I had two numbers 3 on my list
java sql hibernate
digiarnie
source share