How to get last record from Mysql using Hibernate?

独自空忆成欢 提交于 2019-12-02 20:55:31

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();

When you're using HQL, you should specify fully qualified className instead of tableName. The same way you should specify propertyName instead of columnName. Also keep in mind that both are case - sensitive.

Looking at your queries & the exception you're getting, I'm assuming that lahetys is your table name & lahetysNro is your column name.

You should use for example: If your Lahetys class is located at com folder:

List<Lahetys> last = session.createQuery("from com.Lahetys order by lahetysNro DESC LIMIT 1").list();

For your 2nd question:

Here you have used SQL instead of HQL. When you use SQL with hibernate in such a way, it always returns List<Object[]> & not List<Lahetys[]>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!