1

我正在尝试使用带有 ebeans 的 play 2.4 来实现类似于计算机数据库示例的 crud。一切正常,除了更新。当我针对相同的 MariaDB 和较旧的 ebeans (3.2.2) 运行计算机数据库示例时,它确实更新得很好,因此数据库似乎没有问题。尽管在使用 ebeans 时发现 Maria JDBC 驱动程序存在错误,但我正在使用 MySQL 连接器。

这段代码:

    /**
     * Handle the 'edit form' submission 
     *
     * @param id Id of the user to edit
     */
    public Result update(Long id) {
        Form<User> userForm = form(User.class).bindFromRequest();
        if(userForm.hasErrors()) {
            return badRequest(editForm.render(id, userForm));
        }

        User userFromForm = userForm.get();
System.out.println(userForm.data());
        userFromForm.update();

        flash("success", "User " + userForm.get().alias + " has been updated");
        return GO_HOME;
    }

给出此错误:[OptimisticLockException:数据已更改。更新 [0] 行 sql[更新用户集别名 =?,电子邮件 =?,密码 =?,活动 =?,last_update =?,user_type_id =?其中 id=?] 绑定 [null]]

实体定义如下

@Entity 公共类用户扩展模型 {

private static final long serialVersionUID = 1L;

    @Id
    public Long id;

    @Constraints.Required       
    public String alias;

    @Constraints.Required       
    public String email;

    @Constraints.Required       
    public String password;

    @Constraints.Required       
    public char active;

    @ManyToOne
    public UserType userType;

    @Version
    @Column(columnDefinition = "timestamp default '2014-10-06 21:17:06'")
    public Timestamp lastUpdate;

首先,版本没有按预期放置在 where 子句中。此外,还会引发乐观锁定错误。

我正在连接到 MySQL。所有其他操作,保存,删除等都可以正常工作。这又破了?

插件看起来像

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.4")

// Web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-web" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-webdriver" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-js-engine" % "1.0.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")

// Play enhancer - this automatically generates getters/setters for public fields
// and rewrites accessors of these fields to use the getters/setters. Remove this
// plugin if you prefer not to have this feature, or disable on a per project
// basis using disablePlugins(PlayEnhancer) in your build.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")

// Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using
// enablePlugins(SbtEbean). Note, uncommenting this line will automatically bring in
// Play enhancer, regardless of whether the line above is commented out or not.
 addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")

 //Eclipse
 addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

build.sbt 是

name := """mecamu-play"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean, SbtWeb, PlayEnhancer)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs
)


// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

k in run := true



fork in run := true

任何帮助,将不胜感激。我很想像以前那样使用 ebean 的绑定表单更新。非常感谢

4

3 回答 3

2

OptimisticLockException 的意思是更新语句更新了 0 行。这意味着 where 子句与现有行不匹配。这意味着您需要查看 id 和 version 列的特定绑定值:其中 id=? 和版本=?

如果您有 Ebean 登录(请参阅http://ebean-orm.github.io/docs/setup/logging),那么日志记录将包括绑定值。

一旦您知道更新的 where 子句中使用的绑定值,您需要确定它们为什么不正确(为什么会导致 0 行更新)......那么使用表单在 bean 上设置了哪些值。

对我来说,这些是您需要遵循的步骤。

PS:仅仅因为您在 Stackoverflow 中记录问题并不意味着合适的人会查看您的问题。Playframework 和 Ebean 都有可以寻求帮助的论坛。

于 2016-01-05T21:37:07.980 回答
0

Well it turns out that this is likely a bug with ebeans 4.6.2 https://github.com/playframework/play-ebean/issues/44 I will see if I can use a later version where the issue is fixed like 4.7.2 or later. Since Play is using a plugin model for ebeans now, I am not quite sure how to control which version is pulled in. I am fairly new to scala/sbt. This bug with 4.6.2 explains why earlier versions seem to update fine.

于 2015-12-09T20:06:05.863 回答
0

尝试查看您的文件 1.sql 并检查该列是否存在或尝试查询,例如describe your_table,如果它不存在尝试删除表并再次重新导入相同的表脚本 SQL

于 2017-02-21T16:31:04.370 回答