java - 错误 : org. hibernate.hql.internal.ast.ErrorCounter - 第 1:45 行:意外标记:从表中获取最后一条记录时的 DESC

标签 java spring hibernate hql

我正在尝试使用下面的 hibernate 查询从数据库中获取最后一条记录

//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”或“desc”)和升序(“ASC”或“asc”)只能与order by子句一起使用。

所以你的代码必须类似于:

Query query = session.createQuery("select t.currentfile from file t 
  ORDER BY t.time_created DESC");

(time_created 只是一个示例字段,因为您尚未添加表中的字段或按降序排序的字段)

关于java - 错误 : org. hibernate.hql.internal.ast.ErrorCounter - 第 1:45 行:意外标记:从表中获取最后一条记录时的 DESC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39417054/

相关文章:

java - 与jms交换的接口(interface)

spring - 添加 expire 到 Spring Security remember me cookie

java - 自定义权限评估器

java - hibernate : @ValueGenerationType applied at insert but not update time

hibernate - Spring框架从3.0.7升级到5.0.2

java - Estado HTTP 500-请求处理失败;嵌套的异常是org.hibernate.HibernateException:当前 session 找不到 session

java - Android随机对象

java - 如何从项目文件夹中获取文件

java - Spring AOP : Before method execution with Collection

java - 如何在 Maven 项目中打包资源?