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));
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));
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();
you could do that like this:
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql= "select b.wcd, a.optime from UseWaterRecord a, WellBasicInfo b where a.stcd=:a_stcd and b.stcd=:b_stcd ORDER BY a.optime desc";
Query query = session.createQuery(sql);
query.setString("a_stcd", "10100405");
query.setString("b_stcd", "10100405");
query.setFirstResult(0);
query.setMaxResults(1);
List wrwmList = query.list();
Criteria queryCriteria = currentSession.createCriteria(Project.class);
queryCriteria.setFirstResult(0);
queryCriteria.setMaxResults(1);
entity = (Project) queryCriteria.uniqueResult(); // Casting to the Entity class
public Criteria setFirstResult(int firstResult)
This method takes an integer that represents the first row in your result set, starting with row 0. REFER
setFirstResult
use to get start row but it gives all rows from below that. for that must have to use setMaxResults
.
Jul 24, 2013 at 5:24
setFirstResult
explicit in conjunction with setMaxResults
?