我正在尝试使用带有 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 的绑定表单更新。非常感谢