2
votes

I am quite new with Play Framework, and I am working on an existing project to solve some issues.

My problem is the following: when updating some values (the last edited User and last edited Date) in the database using the Ebean update() function I had a "OptomisticLockException". After trying several solutions I found on the internet, the one working was to use a raw sql query as explained here:

Update query instead of select + save

PlayFramework 2 + Ebean - raw Sql Update query - makes no effect on db

Using this, I have no problem with the Date, but when I want to update the User as well I get the following error: "[PersistenceException: No ScalarType registered for class models.User]"

Here is my query:

String sql = "UPDATE product_release SET lastedited_date= :id2 lastedited_user_id= :id3 WHERE id= :id1";
    SqlUpdate update = Ebean.createSqlUpdate(sql);  
    update.setParameter("id1", productReleaseId);
    update.setParameter("id2", currentDate);
    update.setParameter("id3", currentUser);
    int modifiedCount = update.execute();

The current User is retrieved using the following method:

    public static User findCurrentUser() {
    String currentUserTgi = Context.current().session().get(Constants.TGI);
    return User.find.where().eq(Constants.TGI, currentUserTgi).findUnique();
}

And here is the beginning of User class:

    @Entity
public class User extends Model implements Subject{

@Id
public Long id;

@Required
@Column(unique=true)
public String tgi;

@Required
public String firstName;

@Required
public String lastName;

@ManyToMany
public List<SecurityRole> roles;

public User(String tgi) {
    this.tgi = tgi;
}

public static User.Finder<Long, User> find = new User.Finder<Long, User>(Long.class, User.class);

I have already looked on the internet, but unfortunately I did not find any solution... The closest one would be this StackOverflow question but it concerns a specific problem with Enum values.

Any ideas how I could solve this error ?

It's my first question here, so please be indulgent if I am not clear enough !

Thank you all for your help !! :-)

1

1 Answers

2
votes

Well of course, as always, after spending hours and hours looking for a solution I find it a few minutes after asking for help here -_-

I post it here, just in case someone else has the same problem ! So basically my code had two mistakes:

  1. I forgot a coma in my SQL query, it should be like this:

        String sql = "UPDATE product_release SET lastedited_date= :id2, lastedited_user_id= :id3  WHERE id= :id1";
    
  2. When defining the parameters, I was trying to pass directly the User: in the database in my table "product_release" and the column "lastedited_user_id", the id was inserted as a link to this user in the User table.

That was a wrong thinking, I have to pass the User's ID to the database ! So the set up of the parameter should be like this:

update.setParameter("id3", currentUser.id); 

And that's all folks ! :-)