Aliases expected length is 0; actual length is 1 on hibernate query cache
Asked Answered
B

2

7
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
}

enabling hibernate.cacheable will throw me the following exception

aliases expected length is 0; actual length is 1

Bandaid answered 11/9, 2014 at 13:48 Comment(7)
i assume this is a bug in hibernate : hibernate.atlassian.net/browse/HHH-9111Bandaid
hibernate.atlassian.net down for me ( its always down :( ). Mind pasting the information ?Homocercal
it works now. if not i can paste content hereBandaid
Yeah I saw, we had to rewrite our queries to be named. Did you manage another solution ?Homocercal
For me the result set was quite low and we had more than enough memory. So used maps to manage the cacheBandaid
@Bandaid can please tell me, how it is working? what changes you did? I have facing the same issue. Please revert backBedcover
@Bedcover we just disabled caching (as shown in code above we are not suing it any more). Instead of caching it in hibernate layer we manually cache it in a LRU map.Bandaid
P
1

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.

And so... The environment

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

So, first using

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

I received an error similar to this

aliases expected length is 0; actual length is 1

but, of course, with different numbers (instead of 0 and 1)

Note: my native sql was like this

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...

Tried this

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

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.

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

to

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

and got the same error similar to

aliases expected length is 0; actual length is 1

And then I tried this

em.createNamedQuery("MyClass.findMyStuff", Tuple.class)
            .setParameter(0, declarationDate)
            .setHint("org.hibernate.cacheable", true)
            .unwrap(NativeQuery.class)
            .addScalar("mydate", new DateType())
            .getResultList()

And BAAAAAAM finally it started working.

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!!!

Related threads (to name a few)

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

Paduasoy answered 1/9, 2020 at 18:15 Comment(0)
K
0

I think this happens when the queried table does not have a primary key.

Kalb answered 26/6, 2015 at 12:9 Comment(2)
is this the solutions, we are facing same issue,Heparin
As its in the comment in for the qestion, its a bug in hibernate ( not a feature ). hibernate.atlassian.net/browse/HHH-9111Bandaid

© 2022 - 2024 — McMap. All rights reserved.