I'm using createSQLQuery
with setString
(No hard coded value) in Hibernate. I want to know that is Hibernate uses PreparedStatement
for 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
/* 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.
I have tried to execute a query using Hibernates' createSQLQuery method then it will give me exception as below :-
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.
I have also found a question by @Alex Serna at Hibernate createSQLQuery parameter substitution where Alex also get the exception while trying to substitute table name.
Observing stack trace I think Hibernate uses PreparedStatement for createSQLQuery internally.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With