Hibernate查询缓存适用于本机查询吗? [英] Hibernate query cache applicable for native queries?

查看:47
本文介绍了Hibernate查询缓存适用于本机查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码段中使用了休眠查询.它工作正常.当我第二次执行此代码时时间查询未触发,因为它是从查询缓存中选择的

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

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

但是,只要我将上面代码片段中的line1替换为下面的行,我就会得到异常

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");

执行是

    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?

更新:-我认为查询缓存适用于HQL,不适用于本机SQL.但是在休眠文档中,它没有写在任何地方.所以,我只是想知道我在这里什么都没想

Update:- 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

推荐答案

SQLQuery不支持缓存.它可以帮助您.

SQLQuery not support cache. It could help you.

http://www.tutorialspoint.com/hibernate/hibernate_caching.htm

最好使用休眠SQL sintaxis.

It's better use the hibernate SQL sintaxis.

这篇关于Hibernate查询缓存适用于本机查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆