getting first row of table by criteria query

Nikhil Mishra picture Nikhil Mishra · Jul 24, 2013 · Viewed 38.1k times · Source

How can I get the first row of a table by using criteria or HQL query?

Table creation script

   CREATE TABLE MonthlySubscriber(MSISDN bigint(20) 
   NOT NULL, MonthOfYear int(11) NOT NULL, 
   PRIMARY KEY (MSISDN)); 

Answer

Jubin Patel picture Jubin Patel · Jul 24, 2013

Yes you can do that with setMaxResults & setFirstResult in criteria

Sample Code

Criteria queryCriteria = session.createCriteria(MonthlySubscriber.class);
queryCriteria.setFirstResult(0);
queryCriteria.setMaxResults(1);
monthlySubscriberList = queryCriteria .list();