Does hibernate's CreateSQLQuery use a prepared statement?

I am using createSQLQuery with setString (without a hard coded value) in Hibernate. I want to know that Hibernate uses PreparedStatement for createSQLQuery ?

Concern:

I want to save the execution plan created by this request in the cache the next time the same request is launched in the database. He will use the same execution plan.

FYI: I am 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 could not find a link to stackoverflow or even google, so please provide me a link if one is.

+7
java sql sql-server hibernate
source share
2 answers

I tried to execute the query using the createSQLQuery Hibernates method, then it will give me an exception as shown 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, it is clear that he will try to execute com.mysql.jdbc.PreparedStatement.executeQuery (PreparedStatement.java:2275) internally.

I also found the @Alex Serna question in Hibernate createSQLQuery with parameter substitution , where Alex also gets an exception when trying to replace the table name.

Stack Trace Monitoring I think Hibernate uses PreparedStatement to createSQLQuery internally .

+8
source share

Hibernate SQLQuery bypasses the Hibernate session cache and database queries ONLY. You can read more @ Hibernate: SQL vs HQL with session cache .

+1
source share

All Articles