Update method of ebean does not work in playframework
Asked Answered
E

5

9

Recently I began using Play framework 2.3.8.

However update of model is I am in trouble without work well.

By the way, save method works.

Update does not work with code like the following. (It will not be persisted to the database.)

User user = User.findByEmail(email);
user.remoteAddress = remoteAddress;
user.userAgent     = userAgent;
user.latitude      = latitude;
user.longitude     = longitude;
user.lastLoginAt   = new Date();
user.update();

However the following code will work as intended.

User newUser = new User();
newUser.id = user.id;
newUser.remoteAddress = remoteAddress;
newUser.userAgent     = userAgent;
newUser.latitude      = latitude;
newUser.longitude     = longitude;
newUser.lastLoginAt   = new Date();
newUser.update();

Why can not I update the original instance?

User class is as follows.

package models.entities;

import java.util.*;

import javax.persistence.*;

import com.avaje.ebean.annotation.*;

import play.db.ebean.*;
import play.db.ebean.Model.Finder;

import play.data.validation.Constraints.*;

import play.Logger;

import models.services.*;

/**
* @author satouf
*/
@Entity
@Table(name="user")
public class User extends Model {

  @Id
  public Long id;

  @Column(nullable = false, columnDefinition = "varchar(32)")
  public String userIdentifier;

  @Column(nullable = false, columnDefinition = "varchar(255)")
  public String loginId;

  @Column(columnDefinition = "text")
  public String loginPassword;

  @Column(columnDefinition = "varchar(64)")
  public String handleName;

  @Email
  @Column(nullable = false, columnDefinition = "varchar(255)")
  public String email;

  @Column(nullable = false, columnDefinition = "smallint default 0")
  public short status;

  @Column(columnDefinition = "varchar(64)")
  public String lastRemoteAddress;

  public String lastUserAgent;

  public Date lastLoginAt;

  public double latitude;

  public double longitude;

  @UpdatedTimestamp
  public Date updatedAt;

  @CreatedTimestamp
  public Date createdAt;

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

  @Override
  public String toString(){
    return ("[id: " + id + ", userIdentifier: " + userIdentifier +     ",     loginId: " + loginId + ", handleName: "
      + handleName + ", latitude: " + latitude + ", longitude: " +     longitude + "]");
  }

}
Engird answered 7/3, 2015 at 14:29 Comment(6)
What is the database engine? isn't user a reserved word in it ? try to use @Table(name="users") insteadDecided
I am using mysql.'user' is, I think is not a restricted word In mysql.Engird
Do you get an error after issuing the user.update() function? Or is it simply just not persisted to the database?Drysalter
The latter. Error when updating has not occurred. But it is not persisted in the database.Engird
I tried to change to "users" the name of @Table annotation, but does not change the situation. I try a little more research.Engird
Look here #11700257Pawl
J
19

For some strange reason direct access to members fails for ebean update.

Add setters, for example:

private setUserAgent(String val) {  
    this.userAgent = val;  
}  

and call:

user.setUserAgent(your_val);  
user.update();  
Java answered 7/10, 2015 at 8:22 Comment(7)
Thank you answer. This matter has been resolved After upgrading to version 2.4.Engird
@Fuyuhiko Satou: I am using Play 2.4.x. This problem is not solved yet. That is why google brought me herePadriac
@Nissassin17 This problem seems to or did not occur or occur. In my case, it was solved by chance in the upgrade, it did not occur in linux and mac. (It occurs only on Windows)Engird
@FuyuhikoSatou: I am using Mac (Yosemite). My object was udpated only after changing from direct assignment to overwritten getter.Padriac
@Nissassin17 If you have made another application using activator new, whether the same phenomenon occurs?Engird
This worked for me too! I found solution by myself, and see this post while searching if there is an already accepted answer. I up-voted this answer, I hope it helps other people..Vogue
Even I am surprised, that it worked for me after using setters for the model class. It didn't work otherwise. Can anyone kindly explain what's happening under the hood?Disfeature
S
5

Perform a full rebuild. The issue happens when you have an existing Model class (like User) and you add a new field. If you do not clean compile, the PlayEnhancer will not generate setters/getters for the new field, therefore it fails to do the update. A clean compile will do the trick. More info: https://www.playframework.com/documentation/2.6.x/PlayEnhancer

Schilling answered 7/8, 2017 at 6:3 Comment(0)
V
4

Do clean and recompile

This is because you're having an issue with bean property not being updated.

Vivyanne answered 13/1, 2017 at 19:47 Comment(0)
D
0

For updating objects add the ID as a param for update method, i.e.:

user.update(user.id);

for creating ne objects use save() method without param:

newUser.save();
Decided answered 7/3, 2015 at 23:18 Comment(5)
Thank you for answer. I was modified as follows. user.update(user.id); But, the error has occurred. a public id exists in the User class. The error message below. Cannot invoke the action, eventually got an error: org.springframework.beans.InvalidPropertyException: Invalid property 'id' of bean class [models.entities.User]: No property 'id' foundEngird
Show us your User modelDecided
Sorry, I'm not able to reproduce your problem, if you can, publish it somewhere i.e. on githubDecided
Thank you for answer. I'll try to examine a little more.Engird
Hi, I had the same problem. save() method triggers either insert() or update() method accordingly. This didn't fix the problem. I noticed using setters fixes problem, instead of field access, as descripted in Tamir's answer. It is better to remove this suggestion biesior, unless there is a case you had that save() and update() methods work differently. FYI and Thanks.Vogue
A
0

In my case, I had to stop using Azul Java (11) and use Oracle Java (11) on Apple M1 Ventura.

Weird!

Assr answered 2/4 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.