【发布时间】: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();
}
}
【问题讨论】:
-
完成了吗?它运行吗?它会抛出异常吗? (只是假设您遗漏的代码不会削弱操作)。你调试了吗?
-
它工作正常,但数据库中没有变化。我没有堆栈跟踪。此查询的结果是表中的行数。没有例外,什么都没有