更新查询,工作正常但不是 i dao class

Update query, works fine but not i dao class

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

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

Dao 看起来像这样:

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();
        }
    }

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

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

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

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