如何使用Hibernate从Mysql最后一个记录?
技术问答
155 人阅读
|
0 人回复
|
2023-09-14
|
List last = session.createQuery("from lahetys order by lahetysNro DESC LIMIT 1").list();在日志里,我得到了:3 d: f+ |. T! o9 x& V c
INFO: 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处理这个问题的最佳实践是什么?
3 {. W" A+ u% s) o另一个问题:/ X* H3 G: h0 S
Lahetys last = (Lahetys)session.createSQLQuery("select * from lahetys order by lahetysNro DESC LIMIT 1").uniqueResult();session.getTransaction().commit();我得到了一个例外:1 ]% @: O: I. b' [7 ~* k) G9 F
Ljava.lang.Object; cannot be cast to Lahetys所以我不能把对象投射到我身上Lahetys对象上,很奇怪吗?
8 ]7 \# d2 H8 ? K/ ~' ?$ N谢谢!萨米族
& G5 i P! d2 r/ F: W
6 F; ]4 j/ D5 a 解决方案:
* r8 R3 \ w. e# P: U 您的HQL查询无效。LIMIT不是有效的HQL子句。要在Hibernate只要做到这一点
8 K1 {) J! N% j! D+ `& s) QQuery query = session.createQuery("from lahetys order by lahetysNro DESC");query.setMaxResults(1);Lahetys last = (Lahetys) query.uniqueResult();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|
|
|
|
|