Reputation: 131
I am trying to implement crud sort of like computer-database example using play 2.4 with ebeans. Everything works except update. When I run the computer-database example against same MariaDB and older ebeans (3.2.2), it does update just fine so does not seem to be an issue with the database. I am using MySQL connector though as found bug with Maria JDBC driver while using ebeans.
This code:
/**
* 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;
}
gives this error: [OptimisticLockException: Data has changed. updated [0] rows sql[update user set alias=?, email=?, password=?, active=?, last_update=?, user_type_id=? where id=?] bind[null]]
The entity is defined like
@Entity public class User extends Model {
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;
First, the version is not being placed in the where clause as expected. Also, the optimistic locking error is thrown.
I am connecting to MySQL. All other actions, save, delete etc. work fine. Is this broke again?
plugins look like
// 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 is
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
Any help would be appreciated. I would love to use bound form updates with ebean the way they worked before. Thanks so much
Upvotes: 1
Views: 1011
Reputation: 23
Try to see your file 1.sql and check if the column exist or try a query such as describe your_table
, if it does not exist try to drop table and re import again the same table script SQL
Upvotes: 0
Reputation: 4021
What the OptimisticLockException is saying is that update statement updated 0 rows. What that means is that the where clause did not match an existing row. What that means is you need to look at the specific bind values for the id and version columns: where id=? and version=?
If you have Ebean logging on (refer http://ebean-orm.github.io/docs/setup/logging) then the logging will include the bind values.
Once you know the bind values used in the where clause of the update you need to determine why they are not correct (why it results in 0 rows updated) ... so what values are set onto the bean using the form.
To me those are the steps you need to follow.
PS: Just because you log an issue in Stackoverflow does not mean the right people look at your issue. Both Playframework and Ebean have forums you can seek help on.
Upvotes: 2
Reputation: 131
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.
Upvotes: 0