15

如何通过使用criteriaHQL查询获取表的第一行?

表创建脚本

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

4 回答 4

31

是的,您可以使用setMaxResults& setFirstResultin 标准来做到这一点

示例代码

Criteria queryCriteria = session.createCriteria(MonthlySubscriber.class);
queryCriteria.setFirstResult(0);
queryCriteria.setMaxResults(1);
monthlySubscriberList = queryCriteria .list();
于 2013-07-24T05:13:21.540 回答
4

你可以这样做:

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();
于 2017-05-26T02:34:28.270 回答
0
 Criteria queryCriteria = currentSession.createCriteria(Project.class);
 queryCriteria.setFirstResult(0);
 queryCriteria.setMaxResults(1);
 entity = (Project) queryCriteria.uniqueResult(); // Casting to the Entity class
于 2020-09-06T14:03:04.910 回答
-1
public Criteria setFirstResult(int firstResult)

此方法采用一个整数,表示结果集中的第一行,从第 0 行开始。REFER

于 2013-07-24T05:10:24.523 回答