{"action":"created","issue":{"url":"https://api.github.com/repos/ebean-orm/avaje-ebeanorm/issues/459","labels_url":"https://api.github.com/repos/ebean-orm/avaje-ebeanorm/issues/459/labels{/name}","comments_url":"https://api.github.com/repos/ebean-orm/avaje-ebeanorm/issues/459/comments","events_url":"https://api.github.com/repos/ebean-orm/avaje-ebeanorm/issues/459/events","html_url":"https://github.com/ebean-orm/avaje-ebeanorm/issues/459","id":116490717,"number":459,"title":"不知道 ebean 有没有带 where 条件的更新","user":{"login":"liuanxin","id":2647495,"avatar_url":"https://avatars.githubusercontent.com/u/2647495?v=3","gravatar_id":"","url":"https://api.github.com/users/liuanxin","html_url":"https://github.com/liuanxin","followers_url":"https://api.github.com/users/liuanxin/followers","following_url":"https://api.github.com/users/liuanxin/following{/other_user}","gists_url":"https://api.github.com/users/liuanxin/gists{/gist_id}","starred_url":"https://api.github.com/users/liuanxin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/liuanxin/subscriptions","organizations_url":"https://api.github.com/users/liuanxin/orgs","repos_url":"https://api.github.com/users/liuanxin/repos","events_url":"https://api.github.com/users/liuanxin/events{/privacy}","received_events_url":"https://api.github.com/users/liuanxin/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":12,"created_at":"2015-11-12T06:47:18Z","updated_at":"2015-11-24T08:37:21Z","closed_at":"2015-11-23T12:22:25Z","body":"如果有一张订单表, 需要将状态由已创建变更为已支付, 对应的 sql 语句是:\r\n```sql\r\nupdate t_order set status = 'Pay' where id = xxx and status = 'Create'\r\n```\r\n然后根据返回的结果是否为 1 来判断更新有没有成功, 从而决定是否将事务进行回滚.\r\n\r\n不想使用 version 的原因在于: 如果有其他的地方在做跟 status 无关的更新操作, 这里的更新将会失败, 还是想基于这一个属性做这种颗粒度的更新, 而且从性能的角度来说, 也多了一次 load version 的查询\r\n```sql\r\nv = select version from t_order where id = xxx\r\n\r\nupdate t_order set status = 'Pay', version = v + 1\r\nwhere id = xxx and version = v\r\n```\r\n\r\n虽然也能用 createQuery 的方式完成这个实现\r\n```java\r\npublic void pay(Long id) {\r\n    String delete = \"update t_order set status = :new and id = :id and status = :old\";\r\n    SqlUpdate update = ebeanServer.createSqlUpdate(delete);\r\n    update.setParameter(\"id\", id);\r\n    update.setParameter(\"old\", \"Create\");\r\n    update.setParameter(\"new\", \"Pay\");\r\n\r\n    if (update.execute() != 1) {\r\n        // rollback...\r\n        return;\r\n    }\r\n}\r\n```\r\n但是, 在代码中嵌入 SQL, 多少还是挺难受的, 一旦表名或字段名发生变更(尽管这种情况并不多见)就难受了, 虽然可以挪到 xml, 但是在我看来那样更难受.\r\n\r\n我的意思是, 能不能在 ebeanServer 中添加一个像下面这样的方法:\r\n```sql\r\npublic interface EbeanServer {\r\n    ...\r\n    int update(Object newBean, Object oldBean) throws OptimisticLockException;\r\n    ...\r\n}\r\n```\r\n\r\n这样一来, 调用时, 我只需要\r\n```java\r\nOrder order = new Order();\r\norder.setStatus(\"pay\");\r\n\r\nOrder old = new Order();\r\nold.setId(id);\r\nold.setStatus(\"Create\");\r\n\r\nif (ebeanServer.update(order, old) != 1) {\r\n    log...\r\n\r\n    // trigger transaction rollback or do anything with business-related operations\r\n    throw new ServiceException(\"change status fail. please check! order_id: {}\", id);\r\n}\r\n```\r\n\r\n当然, 可能带 where 的条件规则会很多, 不仅仅只是这么简单了. 比如\r\n```sql\r\nupdate t_order set status = 'Pay'\r\nwhere id = xxx and (status = 'Create' or status = 'Confirm')\r\n\r\ndelete t_system_log where is_delete = 1 and create_time <= '3 month ago'\r\n```\r\n\r\n会有这种需求, 是因为之前我用的 mybatis, 可以用 example 轻松组装出上面的 sql 出来"},"comment":{"url":"https://api.github.com/repos/ebean-orm/avaje-ebeanorm/issues/comments/159195739","html_url":"https://github.com/ebean-orm/avaje-ebeanorm/issues/459#issuecomment-159195739","issue_url":"https://api.github.com/repos/ebean-orm/avaje-ebeanorm/issues/459","id":159195739,"user":{"login":"zhangshifeng","id":3119627,"avatar_url":"https://avatars.githubusercontent.com/u/3119627?v=3","gravatar_id":"","url":"https://api.github.com/users/zhangshifeng","html_url":"https://github.com/zhangshifeng","followers_url":"https://api.github.com/users/zhangshifeng/followers","following_url":"https://api.github.com/users/zhangshifeng/following{/other_user}","gists_url":"https://api.github.com/users/zhangshifeng/gists{/gist_id}","starred_url":"https://api.github.com/users/zhangshifeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zhangshifeng/subscriptions","organizations_url":"https://api.github.com/users/zhangshifeng/orgs","repos_url":"https://api.github.com/users/zhangshifeng/repos","events_url":"https://api.github.com/users/zhangshifeng/events{/privacy}","received_events_url":"https://api.github.com/users/zhangshifeng/received_events","type":"User","site_admin":false},"created_at":"2015-11-24T08:37:21Z","updated_at":"2015-11-24T08:37:21Z","body":"是不是查询时没有把版本字段查出来,如果是,之后的更新SQL中就不包含版本字段,也不执行乐观锁检查\r\n\r\n> 我现在在不同request请求中分别查询和更新，更新的时候并没有自动更新version字段，为啥？"}}