如何编写 HQL 查询

How to write the HQL query

我有一个数据库table,用来保存教育经历。它有4个属性。

  1. userId(与用户相关的外键table)
  2. beginDay(报名时间)
  3. endDay(毕业时间)
  4. 学校(学校名称)

现在我有一个userId,我想select根据这个userId上次的教育经历

如何写hql查询?

我写了

select * from education where userId = 100 and beginDay = max(beginDay)

但是查询错误。控制台输出无效使用群组功能。

只需使用此查询即可:

SELECT * FROM education WHERE userId = 100 ORDER BY beginDay DESC LIMIT 1
String hql = "select * from education where userId = 100 and beginDay = max(beginDay)";
Query query = session.createQuery(hql);
List results = query.list();
FROM education e where e.UserId = 100 and e.beginDay = max(e.beginDay)

应该可以。我不知道你的数据库到底是什么样子

如果我没记错的话,如果它是外键值,您需要获取完整的 class 结构。好久不见...

FROM education e where e.User.UserId = 100 and e.School.beginDay = max(e.beginDay)