【问题标题】:Update query, works fine but not i dao class更新查询,工作正常,但不是 i dao 类
【发布时间】:2016-01-22 11:03:42
【问题描述】:

相同的查询在 mysql workbrench 中运行良好,但是当我在程序中运行它时没有结果,甚至没有错误。真的不知道如何解决这个问题。

我正在使用休眠 5.0.0 和 mysql 连接器 5.0.5

道长这样:

Session session = null;
    Transaction tx = null;

    try {
        session = HibernateAnnotationUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();
        String hql = "UPDATE cms_asset SET `value` = CASE application_structure_id "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? "
                + "ELSE `value` END WHERE application_structure_id > 0;";
        Query query = session.createSQLQuery(hql);
        query.setParameter(0, "asd");
        ...
        query.setParameter(26, "asd2");
        Integer result = query.executeUpdate();
        tx.commit();
        if(result > 0){
            return true;
        }else {
            return false;
        }   

    } catch (Exception e) {
        try {
            tx.rollback();
        } catch (RuntimeException rbe) {
            rbe.printStackTrace();
        }
        e.printStackTrace();
        return null;
    } finally {
        if (session != null) {
            session.close();
        }
    }

【问题讨论】:

  • 完成了吗?它运行吗?它会抛出异常吗? (只是假设您遗漏的代码不会削弱操作)。你调试了吗?
  • 它工作正常,但数据库中没有变化。我没有堆栈跟踪。此查询的结果是表中的行数。没有例外,什么都没有

标签: java mysql hibernate dao


【解决方案1】:

据我所知,您必须使用SQLQuery 接口来处理本机 SQL 更新。例如

    SQLQuery query = session.createSQLQuery(hql);
    query.setParameter(0, "asd");
    ...
    query.setParameter(26, "asd2");
    Integer result = query.executeUpdate();

否则,您可能需要使用Custom SQL Annotations。但 HQL 将是最好的选择。

【讨论】:

    【解决方案2】:

    问题的解决方案是将引擎表从 InnoDB 更改为 MyISAM,然后更新工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      相关资源
      最近更新 更多