gpt4 book ai didi

java - 别名预期长度为 0; hibernate 查询缓存的实际长度为 1

转载 作者:搜寻专家 更新时间:2023-11-01 03:51:41 24 4
gpt4 key购买 nike

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 会抛出以下异常

aliases expected length is 0; actual length is 1

最佳答案

所以,这也发生在我身上,在搜索/研究了一个可能的解决方案之后,我已经决定放弃,但经过很长的调试 session (不是 Session.class)我迷迷糊糊解决方案。

等等……环境

hibernate-core 5.3.0.Final

Wildfly 20.0.1 Final

Java 11

PostgreSql 9.4 ? (don't know the exact version, sorry)

Infinispan for caching

所以,首先使用

em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
.setParameter(0, declarationDate)
.setHint("org.hibernate.cacheable", true)
.getResultList()

我收到类似这样的错误

aliases expected length is 0; actual length is 1

但是,当然,有不同的数字(而不是 0 和 1)

注意:我的原生sql是这样的

select a.date as mydate, a.id as myid, a.test as mytest from mytable a where date = ?0

所以,风扇上有一股臭味...

试过了

em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
.setParameter(0, declarationDate)
.setHint("org.hibernate.cacheable", true)
.unwrap(NativeQuery.class)
.addSynchronizedQuerySpace("myQuery_space")
.getResultList()

我失败了...用所有可用的 addSynchronized 方法尝试了同样的东西NativeQuery.class,但每次 hibernate 都告诉我去自娱自乐。

然后我尝试用 HQL 重写我的查询,但祝你好运......重写所有这些子选择、连接以及 HQL 中没有的东西只是一厢情愿的想法——不可能(在我的案件。请注意,我拥有的资源是有限的)。

回到我开始的地方......并改变了这个

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

得到了类似的错误

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 终于开始工作了。

总而言之,它开始工作,然后我将查询解包到 NativeQuery 并添加标量有类型!!!

请注意,如果您的 native 查询没有别名,请添加它们!!!

相关主题(仅举几例)

https://hibernate.atlassian.net/browse/HHH-9111

Hibernate Ehcache NOT working for SQL Native query cache

Hibernate query cache applicable for native queries?

Cache JPA Native Query

关于java - 别名预期长度为 0; hibernate 查询缓存的实际长度为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25789176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com