-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate query cache applicable for native queries?
PostPosted: Mon Mar 25, 2013 8:15 am 
Newbie

Joined: Mon Mar 25, 2013 8:12 am
Posts: 2
I have below code snippet where i am using hibernate query. It works fine. when i fire execute this code second
time query is not fired as it is picked from query cache

Code:
   Session session = hibernateFactory.openSession();
        Query employeeQuery=session.createQuery("from Employee where employeeId=1");//line 1
        employeeQuery.setCacheable(true);
        Employee employee1=(Employee)employeeQuery.uniqueResult();
        session.close();


But as soon as i replace line1 in above code snippet with below line i get exception

Query employeeQuery=session.createSQLQuery("select * from Employee where employeeId=1");


Execption is

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at org.hibernate.type.TypeHelper.disassemble(TypeHelper.java:146)
at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:106)
at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2434)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2321)
at org.hibernate.loader.Loader.list(Loader.java:2268)

Is query cache is not applicable for native queries?

i think Query cache is applicable for HQL not for Native SQL. But in hibernate documentation its not written anywhere. So just wondering am i missing anything here


Top
 Profile  
 
 Post subject: Re: Hibernate query cache applicable for native queries?
PostPosted: Wed Mar 27, 2013 10:02 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

when you use session.createSQLQuery
then you must tell hibernate to which entity class the result should be mapped.
Try as follows:


Quote:
Query employeeQuery = session.createSQLQuery("select e.* from Employee e where employeeId=1");
employeeQuery.addEntity("e", Employee.class);
employeeQuery.setCacheable(true);
Employee employee1=(Employee)employeeQuery.uniqueResult();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.