ERROR: org.hibernate.hql.internal.ast.ErrorCounter - line 1:45: unexpected token : DESC on fetching the last record from the table
ERROR: org.hibernate.hql.internal.ast.ErrorCounter - line 1:45: unexpected token : DESC on fetching the last record from the table
我正在尝试使用以下休眠查询从数据库中获取最后一条记录
//fetch the last data from a field in a table
Session session = _sessionFactory.getCurrentSession();
Query query = session.createQuery("select t.currentfile from file t DESC");
query.setMaxResults(1);
List<Object[]> rows = query.list();
for (Object[] row: rows) {
System.out.println(" ------------------- ");
System.out.println("current file: " + row[0]);
}
使用上面的 hql 我得到这个错误:
ERROR: org.hibernate.hql.internal.ast.ErrorCounter - line 1:45: unexpected token
: DESC
有什么问题吗?
您的查询有误。您必须在 DESC 之前放置一个 ORDER BY yourSortAttribute。
降序('DESC' 或 'desc')和升序('ASC' 或 'asc')只能与 order by[=19] 一起使用=] 子句。
因此您的代码必须类似于:
Query query = session.createQuery("select t.currentfile from file t
ORDER BY t.time_created DESC");
(time_created
只是一个示例字段,因为您还没有添加 table 中的字段或按降序排序的字段)
我正在尝试使用以下休眠查询从数据库中获取最后一条记录
//fetch the last data from a field in a table
Session session = _sessionFactory.getCurrentSession();
Query query = session.createQuery("select t.currentfile from file t DESC");
query.setMaxResults(1);
List<Object[]> rows = query.list();
for (Object[] row: rows) {
System.out.println(" ------------------- ");
System.out.println("current file: " + row[0]);
}
使用上面的 hql 我得到这个错误:
ERROR: org.hibernate.hql.internal.ast.ErrorCounter - line 1:45: unexpected token
: DESC
有什么问题吗?
您的查询有误。您必须在 DESC 之前放置一个 ORDER BY yourSortAttribute。
降序('DESC' 或 'desc')和升序('ASC' 或 'asc')只能与 order by[=19] 一起使用=] 子句。
因此您的代码必须类似于:
Query query = session.createQuery("select t.currentfile from file t
ORDER BY t.time_created DESC");
(time_created
只是一个示例字段,因为您还没有添加 table 中的字段或按降序排序的字段)