如何使用Hibernate从Mysql获取最后一条记录? [英] How to get last record from Mysql using Hibernate?
本文介绍了如何使用Hibernate从Mysql获取最后一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
List<Lahetys> last = session.createQuery("from lahetys order by lahetysNro DESC LIMIT 1").list();
并在日志中获得:
and in the log I got:
INFO: Hibernate: select from order by lahetysNro DESC LIMIT 1
WARN: SQL Error: 1064, SQLState: 42000
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from order by lahetysNro DESC LIMIT 1' at line 1
发生了什么事情来自LAHETYS?使用HQL或/和SQL处理这种情况的最佳实践是什么?
What has happend to "from LAHETYS"? What is the best practice to handle that with HQL or/and with SQL?
另一个问题:
Another problem:
Lahetys last = (Lahetys)session.createSQLQuery("select * from lahetys order by lahetysNro DESC LIMIT 1").uniqueResult();
session.getTransaction().commit();
我得到一个异常:
Ljava.lang.Object; cannot be cast to Lahetys
所以我不能将一个物体投射到我的Lahetys物体上,很奇怪吗?
So I can't cast an object to my Lahetys-object, weird?
谢谢!
Sami
Thank you! Sami
推荐答案
您的HQL查询无效。 LIMIT不是有效的HQL子句。为了在Hibernate中做到这一点,只要做到这一点
Your HQL query is invalid. LIMIT is not a valid HQL clause. To do that in Hibernate, just do
Query query = session.createQuery("from lahetys order by lahetysNro DESC");
query.setMaxResults(1);
Lahetys last = (Lahetys) query.uniqueResult();
这篇关于如何使用Hibernate从Mysql获取最后一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文