当我运行以下命令时:
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);
}
Hibernate 总是抛出异常并将以下内容输出到控制台:
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)
当 from Intake i where i.assignedTo=?
显然不是更新时,为什么 Hibernate 在这里给我一个错误?我怀疑它与 IBM DB2 JDBC 驱动程序有关。我使用的是 DB2 驱动程序版本 2.7.58。
这是我的 Spring 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>
最佳答案
- 尝试完整版 - 即
SELECT i FROM Intake i where i.assignedTo=?
; - 如果这不起作用 - 升级 JDBC 驱动程序;
- 如果这不起作用或没有新版本 - 提交 JDBC 驱动程序的错误。
关于java - hibernate 错误 : executeQuery method cannot be used for update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2290557/