Quand je lance ce qui suit:erreur Hibernate: méthode executeQuery ne peut pas être utilisé pour la mise à jour
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 lance toujours une exception et recrache les éléments suivants sur la console:
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.
qui est causée par:
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)
Pourquoi Hibernate me donne une erreur ici quand from Intake i where i.assignedTo=?
est obvio Habituellement pas une mise à jour? Je soupçonne que cela a quelque chose à voir avec le pilote IBM DB2 JDBC. J'utilise DB2 Driver version 2.7.58.
Voici ma configuration 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>
La requête complète n'a rien fait, mais j'ai mis à jour le pilote à 3.57.82 et maintenant ça marche! Je cherchais auparavant une réponse dans Google et j'ai constaté que c'était parce que DB2 ne prenait pas en charge le paramètre 'hibernate.use_sql_comments'. Eh bien, il s'avère que le dernier pilote JDBC le supporte en fait! – Christian