java - hibernate 错误 : executeQuery method cannot be used for update

标签 java hibernate db2

当我运行以下命令时:

    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>

最佳答案

  1. 尝试完整版 - 即 SELECT i FROM Intake i where i.assignedTo=?;
  2. 如果这不起作用 - 升级 JDBC 驱动程序;
  3. 如果这不起作用或没有新版本 - 提交 JDBC 驱动程序的错误。

关于java - hibernate 错误 : executeQuery method cannot be used for update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2290557/

相关文章:

sql - 如何在 DB2 中创建一个返回序列值的函数?

sql - 避免在 DB2 中将 String 转换为 Integer 时出错

java - 实现一个 toString 方法来打印出一个 LinkedList

java - Spring MVC Hibernate 创建名为 'sessionFactory' 的 bean 时出错

java - 如何比较 Java 中的字符串?

mysql - hibernate sql join查询错误: javax. servlet.ServletException:对于输入字符串

java - 一个 javax.persistence.jdbc.url 适用于 Windows 和 Linux,使用 firebird

c# - 流畅的nhibernate级联删除到已连接子类的集合

java - 如何从 Android 中的 PreferenceActivity 获取 SharedPreferences?

bash - 检测 db2 死锁的脚本