Getting the first row of the table by query criteria

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

Create table script

  CREATE TABLE MonthlySubscriber(MSISDN bigint(20) NOT NULL, MonthOfYear int(11) NOT NULL, PRIMARY KEY (MSISDN)); 
+6
source share
3 answers

Yes, you can do this with setMaxResults and setFirstResult in criteria

Code example

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

you can do it 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(); 
0
source
 public Criteria setFirstResult(int firstResult) 

This method takes an integer that represents the first line in your result set, starting at line 0. REFER

-1
source

Source: https://habr.com/ru/post/950149/


All Articles