Select top 1 result using JPA

后端 未结 4 1304
孤独总比滥情好
孤独总比滥情好 2020-12-03 00:49

I need to bring from DB only one single result. How can I do that with JPA?

Select top 1 * from table

I tried

\"sel

4条回答
  •  既然无缘
    2020-12-03 01:13

    Try like this

    String sql = "SELECT t FROM table t";
    Query query = em.createQuery(sql);
    query.setFirstResult(firstPosition);
    query.setMaxResults(numberOfRecords);
    List result = query.getResultList();
    

    It should work

    UPDATE*

    You can also try like this

    query.setMaxResults(1).getResultList();
    

提交回复
热议问题