回答

收藏

如何使用Hibernate从Mysql最后一个记录?

技术问答 技术问答 163 人阅读 | 0 人回复 | 2023-09-14

List last = session.createQuery("from lahetys order by lahetysNro DESC LIMIT 1").list();在日志里,我得到了:
$ Q  ^) i1 ?1 \3 wINFO: Hibernate: select  from order by  lahetysNro DESC LIMIT 1WARN: SQL Error: 1064,SQLState: 42000ERROR: 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处理这个问题的最佳实践是什么?' e; v1 Z% h! Q8 B0 K+ G9 \* V
另一个问题:8 J2 o* b: H5 P) [/ \) ~% _
Lahetys last = (Lahetys)session.createSQLQuery("select * from lahetys order by lahetysNro DESC LIMIT 1").uniqueResult();session.getTransaction().commit();我得到了一个例外:
/ e& S3 P+ r: O! RLjava.lang.Object; cannot be cast to Lahetys所以我不能把对象投射到我身上Lahetys对象上,很奇怪吗?
$ S  @  }$ p! Z/ M6 ^. g; E8 Z: g谢谢!萨米族
! e" I- a# M3 R$ D                                                               
  a. z; J8 o/ \7 B3 t9 p3 D' O+ J    解决方案:                                                                ) F( i- l) s. \4 G  g4 E7 Y' w
                                                                您的HQL查询无效。LIMIT不是有效的HQL子句。要在Hibernate只要做到这一点
2 i/ H( m# _2 A2 B/ c2 J$ NQuery query = session.createQuery("from lahetys order by lahetysNro DESC");query.setMaxResults(1);Lahetys last = (Lahetys) query.uniqueResult();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则