别名的预期长度为0;休眠查询缓存上的实际长度为1 [英] Aliases expected length is 0; actual length is 1 on hibernate query cache
问题描述
String q1Str = "SELECT parent_id FROM meta WHERE st_id = "+childIds+";";
Query q1 = em.createNativeQuery(q1Str);
//q1.setHint("org.hibernate.cacheable", true);
Object parentId = null;
try{
parentId = q1.getSingleResult();
}catch(NoResultException nre){
//nope
}
启用hibernate.cacheable
会抛出以下异常
别名的预期长度为0;实际长度是1
aliases expected length is 0; actual length is 1
推荐答案
所以,这也发生在我身上,在搜索/研究了可能的解决方案之后, 决定放弃,但是经过很长的调试会话(不是Session.class)之后,我绊倒了 在解决方案上.
So, this happened to me too and after searching / researching a possible solution I've decided to give up, but after a very long debug session (not Session.class) I stumbled upon a solution.
等等...环境
休眠核心5.3.0.最终版
hibernate-core 5.3.0.Final
Wildfly 20.0.1最终版
Wildfly 20.0.1 Final
Java 11
PostgreSQL 9.4吗? (不知道确切的版本,对不起)
PostgreSql 9.4 ? (don't know the exact version, sorry)
Infinispan进行缓存
Infinispan for caching
所以,首先使用
em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
.setParameter(0, declarationDate)
.setHint("org.hibernate.cacheable", true)
.getResultList()
我收到与此类似的错误
别名的预期长度为0;实际长度是1
aliases expected length is 0; actual length is 1
但是,当然,它们具有不同的数字(而不是0和1)
but, of course, with different numbers (instead of 0 and 1)
注意:我的本机sql就是这样
Note: my native sql was like this
从mytable中选择a.date作为mydate,将a.id作为myid,将a.test作为mytest,其中date =?0
select a.date as mydate, a.id as myid, a.test as mytest from mytable a where date = ?0
所以,有什么臭气的风扇...
And so, something smelly hit the fan...
尝试过
em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
.setParameter(0, declarationDate)
.setHint("org.hibernate.cacheable", true)
.unwrap(NativeQuery.class)
.addSynchronizedQuerySpace("myQuery_space")
.getResultList()
,我失败了...尝试了所有可用的addSynchronized方法进行相同的操作 NativeQuery.class,但是每次休眠都告诉我去娱乐一下.
and I failed... Tried this same stuff with all addSynchronized methods available in NativeQuery.class, but each and every time hibernate was telling me to go and fun myself.
然后,我尝试用HQL重写查询,但是祝您好运...重写所有这些 亚选择,加入,HQL中没有的只是一厢情愿的想法-不可行(在我 案子.请注意,我的资源有限).
Then I tried rewriting my query with HQL, but good luck with that... Rewriting all these subselects, joins and what not in HQL was just a wishful thinking - NOT POSSIBLE (in my case. Note that resources I have are limited).
回到我的起点...然后更改
Got back to where I've started... And changed this
em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
.setParameter(0, declarationDate)
.setHint("org.hibernate.cacheable", true)
.unwrap(NativeQuery.class)
.addSynchronizedQuerySpace("myQuery_space")
.getResultList()
到
em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
.setParameter(0, declarationDate)
.setHint("org.hibernate.cacheable", true)
.unwrap(NativeQuery.class)
.addScalar("mydate")
.getResultList()
并得到与
别名的预期长度为0;实际长度是1
aliases expected length is 0; actual length is 1
然后我尝试了
em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
.setParameter(0, declarationDate)
.setHint("org.hibernate.cacheable", true)
.unwrap(NativeQuery.class)
.addScalar("mydate", new DateType())
.getResultList()
BAAAAAAM终于开始工作了.
And BAAAAAAM finally it started working.
总而言之,它开始工作,然后我将查询展开为NativeQuery并添加了标量 有类型!!
To sum it up it started working then I unwrapped query to NativeQuery and added scalar with a type!!!
请注意,如果您的本机查询没有别名,请添加别名!!!
Note if your native query does not have aliases then add them!!!
相关线程(仅举几例)
希望此解决方案可以帮助其他迷路的人.
Hope that this solution helps other lost souls.
LOL ...必须创建这个一次性帐户才能发布此答案
LOL... had to create this one time account to post this answer
这篇关于别名的预期长度为0;休眠查询缓存上的实际长度为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!