当我运行以下内容时:
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 Driver有关。我正在使用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>
答案 0 :(得分:2)
SELECT i FROM Intake i where i.assignedTo=?
;