java 休眠中的 createSQLQuery 使用准备好的语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25074167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
createSQLQuery in hibernate uses Prepared Statement?
提问by Vicky Thakor
I'm using createSQLQuery
with setString
(No hard coded value) in Hibernate. I want to know that is Hibernate uses PreparedStatement
for createSQLQuery
?
我在 Hibernate 中使用createSQLQuery
with setString
(无硬编码值)。我想知道 Hibernate 是用来PreparedStatement
做createSQLQuery
什么的?
Concern:
关心:
I want to preserve the execution plan created by this query in cache so next time same query fired on database, It would use same execution plan.
我想在缓存中保留此查询创建的执行计划,以便下次在数据库上触发相同的查询时,它将使用相同的执行计划。
FYI:I'm using MSSQL Server 2008
仅供参考:我使用的是 MSSQL Server 2008
/* This is just example I'm not using same query */
Query nativeSQLQuery = session.createSQLQuery("select Firstname from user_master where user_name = :param");
nativeSQLQuery.setString("param", "vicky.thakor");
I couldn't find stackoverflow link or even in google so please provide me link if any.
我找不到 stackoverflow 链接,甚至在 google 中也找不到,所以请提供链接(如果有)。
回答by OO7
I have tried to execute a query using Hibernates' createSQLQuery method then it will give me exception as below :-
我尝试使用 Hibernates 的 createSQLQuery 方法执行查询,然后它会给我如下异常:-
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
From the above exception we can see that it will try to execute com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275)internally.
从上面的异常我们可以看出,它会尝试在内部执行com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275)。
I have also found a question by @Alex Serna at Hibernate createSQLQuery parameter substitutionwhere Alex also get the exception while trying to substitute table name.
我还在Hibernate createSQLQuery 参数替换中发现了@AlexSerna 提出的一个问题,其中 Alex 在尝试替换表名时也遇到了异常。
Observing stack trace I think Hibernate uses PreparedStatement for createSQLQuery internally.
观察堆栈跟踪我认为Hibernate 在内部使用 PreparedStatement for createSQLQuery。
回答by Amogh
Hibernate SQLQuery bypasses the Hibernate Session cache and queries ONLY against the database. You can read more @ Hibernate: SQL vs HQL with the Session Cache.
Hibernate SQLQuery 绕过 Hibernate Session 缓存并仅查询数据库。您可以阅读更多@Hibernate: SQL vs HQL with the Session Cache。