Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update method of ebean does not work in playframework

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 + "]");
  }

}
like image 938
Fuyuhiko Satou Avatar asked Mar 07 '15 14:03

Fuyuhiko Satou


3 Answers

Do clean and recompile

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

like image 103
Seroney Avatar answered Nov 10 '22 06:11

Seroney


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

like image 34
yerlilbilgin Avatar answered Nov 10 '22 07:11

yerlilbilgin


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();  
like image 40
Tamir Avatar answered Nov 10 '22 07:11

Tamir