同样的查询在mysql workbrench中运行正常,但是当我在程序中运行它没有结果甚至没有错误。真的不知道如何解决这个问题。
我正在使用hibernate 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();
}
}
答案 0 :(得分:0)
据我记得你要使用SQLQuery接口来处理Native SQL Updates。 E.g。
SQLQuery query = session.createSQLQuery(hql);
query.setParameter(0, "asd");
...
query.setParameter(26, "asd2");
Integer result = query.executeUpdate();
否则您可能希望使用Custom SQL Annotations。但是HQL是最好的选择。
答案 1 :(得分:0)
问题的解决方法是将引擎表从InnoDB更改为MyISAM,然后更新工作正常。