如何使用Hibernate从Mysql最后一个记录?
技术问答
457 人阅读
|
0 人回复
|
2023-09-14
|
List last = session.createQuery("from lahetys order by lahetysNro DESC LIMIT 1").list();在日志里,我得到了:
, B% ?2 R: }+ R- q" A% Q6 dINFO: 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处理这个问题的最佳实践是什么?
/ L7 q- {0 i& ~8 k另一个问题:
( G0 _ L% ]8 T' T/ mLahetys last = (Lahetys)session.createSQLQuery("select * from lahetys order by lahetysNro DESC LIMIT 1").uniqueResult();session.getTransaction().commit();我得到了一个例外:& R. @9 Q3 e$ K7 [) G
Ljava.lang.Object; cannot be cast to Lahetys所以我不能把对象投射到我身上Lahetys对象上,很奇怪吗?7 c; q$ b( i% k" W- p
谢谢!萨米族/ F0 e' [9 ~+ b; x. [. N7 h
5 Y8 _. B( P' c: ~9 n) e9 ^ j6 Z( Y
解决方案:
1 u. _1 t: r4 V, I8 v( o' Z! S 您的HQL查询无效。LIMIT不是有效的HQL子句。要在Hibernate只要做到这一点4 x6 ~0 A: x3 i) P3 W
Query query = session.createQuery("from lahetys order by lahetysNro DESC");query.setMaxResults(1);Lahetys last = (Lahetys) query.uniqueResult();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|
|
|
|
|