createSQLQuery in hibernate uses Prepared Statement?

前端 未结 2 1837
刺人心
刺人心 2021-01-12 10:29

I\'m using createSQLQuery with setString (No hard coded value) in Hibernate. I want to know that is Hibernate uses PreparedStatement f

相关标签:
2条回答
  • 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.

    0 讨论(0)
  • 2021-01-12 11:22

    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.

    0 讨论(0)
提交回复
热议问题