2010-02-18 79 views
2

當我運行以下命令:休眠錯誤:executeQuery方法不能用於更新

Session session = null; 
    Transaction tx = null; 
    List<Intake> intakes = null; 
    try{ 
     session = sessionFactory.getCurrentSession(); 
     tx = session.beginTransaction(); 
     intakes = session.createQuery("from Intake i where i.assignedTo=?") 
       .setParameter(0, assignedTo).list(); 
     tx.commit(); 
    } 
    catch(HibernateException e){ 
     tx.rollback(); 
     logger.warn("Unable to list intakes for user " + assignedTo, e); 
    } 

休眠總是拋出異常,吐出以下到控制檯:

6406 [httpSSLWorkerThread-8080-1] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: -99999, SQLState: null 
6406 [httpSSLWorkerThread-8080-1] ERROR org.hibernate.util.JDBCExceptionReporter - 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.hd.a(hd.java:2508) 
     at com.ibm.db2.jcc.a.id.d(id.java:1952) 
     at com.ibm.db2.jcc.a.id.X(id.java:505) 
     at com.ibm.db2.jcc.a.id.executeQuery(id.java:488) 

爲什麼Hibernate給我一個錯誤在這裏時from Intake i where i.assignedTo=?是obvio我們不是更新嗎?我懷疑它與IBM DB2 JDBC Driver有關。我正在使用DB2驅動程序版本2.7.58。

這裏是我的春天Hibernate的配置。

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="mappingResources"> 
      <list> 
       <value>domain/Intake.hbm.xml</value> 
       <value>domain/Action.hbm.xml</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <value> 
       hibernate.dialect=org.hibernate.dialect.DB2Dialect 
       hibernate.current_session_context_class=thread 
       hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider 
       hibernate.show_sql=true 
       hibernate.format_sql=true 
       hibernate.hbm2ddl.auto=create-drop 
       hibernate.use_sql_comments=true 
      </value> 
     </property> 
    </bean> 

回答

2
  1. 嘗試的完整版本 - 即SELECT i FROM Intake i where i.assignedTo=?;
  2. 如果這不起作用 - 升級JDBC驅動程序;
  3. 如果這不工作或沒有新的版本 - 提交一個bug的JDBC驅動程序。
+3

完整查詢並沒有做什麼,但我的司機更新到3.57.82和現在的作品!我環顧四周谷歌的答案之前,發現這是因爲DB2不支持'hibernate.use_sql_comments'設置。那麼,事實證明最新的JDBC驅動程序確實支持它! – Christian 2010-02-18 19:20:07