别名预期长度为 0;休眠查询缓存上的实际长度为 1


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

所以,这也发生在我身上,在搜索/研究可能的解决方案后,我已经决定放弃,但经过很长的调试会话(不是会话.class(后,我跌跌撞撞在解决方案上。

所以...A. 环境

休眠核心 5.3.0.最终版

野蝇 20.0.1 决赛

爪哇 11

PostgreSql 9.4 ?(不知道确切的版本,抱歉(

用于缓存的无限跨

所以,首先使用

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

我收到了与此类似的错误

别名的预期长度为 0;实际长度为 1

但是,当然,使用不同的数字(而不是 0 和 1(

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

从 mytable 中选择 a.date 作为 mydate,a.id 作为 myid,a.test 作为 mytest 其中日期 = ?0

于是,有什么臭气扑在风扇上...

试过这个

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

我失败了...尝试了同样的东西与所有可用的添加同步方法NativeQuery.class,但每次休眠都告诉我去找我玩。

然后我尝试用 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()

并得到类似于

别名的预期长度为 0;实际长度为 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 并添加了标量带类型!!

请注意,如果本机查询没有别名,请添加别名!!

相关主题(仅举几例(

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

休眠 Ehcache 不适用于 SQL 本机查询缓存

休眠查询缓存是否适用于本机查询?

缓存 JPA 本机查询

我认为当查询的表没有主键时会发生这种情况。

最新更新