Search code examples
javasessionplayframeworkplayframework-2.2ebean

Is there a way to get the session information from the Model in PlayFramework?


I create the app by PlayFramework 2.2 with Java.

When saving Model, I want to save the create_user_id / update_user_id as tracking information. But, unable to retrieve the session information from Model of PlayFramework.

Currently, I pass the User entity from Controller, when save the entities. because it is awful implementation, When you call Model.save() method to get a login user from the session in the Model side I want to like us to save implementation.

Please tell me if you know the best practices.

public abstract class AbstractTrailModel extends Model {

  public static final int INSERT = 1;
  public static final int UPDATE = 2;
  public static final int DELETE = 3;

  @ManyToOne(cascade = CascadeType.ALL)
  @Constraints.Required
  public User createUser;

  @ManyToOne(cascade = CascadeType.ALL)
  @Constraints.Required
  public User createUser;

  @Constraints.Required
  public Integer is_delete;


  public void save(User loginUser){

    if(create_time != null){
      setTrailInfo(UPDATE, loginUser);
      super.save();

    }else{
      setTrailInfo(INSERT, loginUser);
      super.save();
    }
  }


  public void setTrailInfo(int code, User loginUser){

    switch(code) {
      case INSERT:
        createUser = loginUser;
      case UPDATE:
      case DELETE:
        updateUser = loginUser;
    }

    if(code == DELETE){
      is_delete = 1;

    }else{
      is_delete = 0;
    }
  }
}

Solution

  • As far as I known its only possible to get the session from a Controller. If you need the User on the model classes, you need the pass the session or the User as an argument everywhere you need.