我想使用JPA Criteria从数据库中获取第一行。我使用JPA,Hibernate 4.2.7。在SQL中,语句如下所示:
SELECT * FROM houses WHERE rownum = 1;
我要实现的Java代码如下所示:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery query = builder.createQuery(House.class);
Root root = query.from(House.class);
query.select(root).where(builder.equal(root.get("rownum"), 1));
TypedQuery tQuery = entityManager.createQuery(query);
House house = tQuery.getSingleResult();
但是’rownum’伪列无法解析,我得到了例外:
java.lang.IllegalArgumentException: Unable to resolve attribute [rownum] against path
at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:229)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:200)
有可能,如果可以,如何使用Criteria API获取“ rownum”伪列?感谢您的任何建议。