For example, if we have a table Books, how would we count total number of book records with hibernate?
Thanks
For example, if we have a table Books, how would we count total number of book records with hibernate?
Thanks
You could try count(*)
Integer count = (Integer) session.CreateQuery("select count(*) from Books").UniqueResult();
Where 'Books' is the name off the class - not the table in the database
return (Number) session.createCriteria("Book").setProjection(Projections.rowCount()).uniqueResult();
It is at least a Number, most likely a Long.
This is what I use:
int countRec = session.createQuery("from City").list().size();
I tried it in about 1000 records and it works just fine.
Uhh. And what is in that list? I think there are selected all entities from that table. Do that on table with billions of record an you will see why this is WERY BAD IDEA!