Google search bar

December 01, 2009

Hibernate's "User SQL Comments" feature causes a problem with DB2

I've been having a problem where a query sent through hibernate fails with the message: executeQuery method cannot be used for update

Here is an example of the log output. The hibernate actual hql and sql queries have been replaced with [...] in order not to expose my employer's database schema information.

Hibernate: /* select [...] */ select [...]JDBCExceptionReporter:100 - SQL Error: -99999, SQLState: nullJDBCExceptionReporter:101 - executeQuery method cannot be used for update.

I googled: db2 "executeQuery method cannot be used for update"

and the second result lead me to this.

It turns out that the problem was that I had the hibernate property "hibernate.use_sql_comments" set to "true". This caused the error because the hql was prepended to the sql query. Once I commented out the setting of that property the problem went away. I suppose that the default is false for this property.


I copy and paste the material here just in case it disappears from the referenced page.

Hibernate problems and solutions

  1. Hibernate not retrieving entity and throwing (what seems) a jdbc driver exception.

I have a simple entity mapping and attempt to save and then retrieve the object by the generated id. Hibernate throws the following exception:

org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not load an entity:
[nz.co.client.blitztecapp.model.MessageArchiveImpl#41]; uncategorized SQLException for SQL [/* load nz.co.client.blitztecapp.model.MessageArchiveImpl */ select messagearc0_.MESSAGE_ARCHIVE_ID as MESSAGE1_0_0_, messagearc0_.MESSAGE_TYPE as MESSAGE2_0_0_, messagearc0_.CONTENTS as CONTENTS0_0_, messagearc0_.MESSAGE_ID as MESSAGE4_0_0_, messagearc0_.PACKAGE_ID as PACKAGE5_0_0_, messagearc0_.CREATED_BY as CREATED6_0_0_, messagearc0_.CREATED_DATE as CREATED7_0_0_ from MESSAGE_ARCHIVE messagearc0_ where messagearc0_.MESSAGE_ARCHIVE_ID=?]; SQL state [null]; error code [-99999]; executeQuery method cannot be used for update.; nested exception is com.ibm.db2.jcc.a.SqlException: executeQuery method cannot be used for update.
Caused by: com.ibm.db2.jcc.a.SqlException: executeQuery method cannot be used for update.
at com.ibm.db2.jcc.a.co.a(co.java:2079)
at com.ibm.db2.jcc.a.cp.d(cp.java:1528)
at com.ibm.db2.jcc.a.cp.K(cp.java:316)
at com.ibm.db2.jcc.a.cp.executeQuery(cp.java:299)
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.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3042)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:467)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:369)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:461)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:455)
.
.

Problem:
The problem is a hibernate property that I had set : hibernate.use_sql_comments=true causes the failure. This seems to be a bug in hibernate - Unset the property and things should work fine.

1 comment:

vitamin b said...

Comments in SQL statements: SQL statements can include host language comments or SQL comments. Either type of comment can be specified wherever a space is valid, except within a delimiter token or between the keywords EXEC and SQL. The following rules apply to SQL comments:
- The two hyphens must be on the same line, not separated by a space.
- Comments cannot be continued on the next line.
- Within a statement embedded in a COBOL program, the two hyphens must be preceded by a blank unless they begin a line.
- In Java, SQL comments are not allowed within embedded Java expressions.