我有一个play framework 2.0.4应用程序,它想要修改数据库中的行。
我需要将数据库中的"少数"消息更新为"已打开"状态(读取消息)我在下面做得很好
String sql = " UPDATE message SET opened = true, opened_date = now() "
+" WHERE id_profile_to = :id1 AND id_profile_from = :id2 AND opened IS NOT true";
SqlUpdate update = Ebean.createSqlUpdate(sql);
update.setParameter("id1", myProfileId);
update.setParameter("id2", conversationProfileId);
int modifiedCount = update.execute();
我已经修改了postgresql来记录所有的查询。
modifiedCount是实际修改的行数,但查询处于事务中。在数据库中完成查询后,会出现ROLLBACK,因此不会进行UPDATE。我尝试过将db更改为H2,结果也是一样的。
这是来自postgres审核日志的查询
2012-12-18 00:21:17 CET : S_1: BEGIN
2012-12-18 00:21:17 CET : <unnamed>: UPDATE message SET opened = true, opened_date = now() WHERE id_profile_to = $1 AND id_profile_from = $2 AND opened IS NOT true
2012-12-18 00:21:17 CET : parameters: $1 = '1', $2 = '2'
2012-12-18 00:21:17 CET : S_2: ROLLBACK
Play Framework文档和Ebean文档-声明没有事务/如果没有声明,或者如果每个查询需要,则为瞬态/。
所以。。。我已经做了的把戏
Ebean.beginTransaction();
int modifiedCount = update.execute();
Ebean.commitTransaction();
Ebean.endTransaction();
Logger.info("update mod = " + modifiedCount);
但这没有什么区别——同样的行为。。。
Ebean.execute(update);
还是一样。。
下一步我做了-我用宣布了这个方法
@Transactional(type=TxType.NEVER)
和
@Transactional(type=TxType.MANDATORY)
他们都没有什么不同。
我对Ebean很失望:(有人能帮忙吗?
顺便说一句。我设置了
Ebean.getServer(null).getAdminLogging().setDebugGeneratedSql(true);
Ebean.getServer(null).getAdminLogging().setDebugLazyLoad(true);
Ebean.getServer(null).getAdminLogging().setLogLevel(LogLevel.SQL);
在Play控制台中查看查询-记录其他查询-此更新-而不是
只需移除初始空间。。。对我也不敢相信。。。从"更新…"更改为"更新。。。
仅此而已。。。
我认为您必须使用原始sql而不是createSqlUpdate语句。