I\'m using createSQLQuery
with setString
(No hard coded value) in Hibernate. I want to know that is Hibernate uses PreparedStatement
f
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.