2015-11-03 148 views
0

保存数据库会话中的值我有一个获取userId和另一个属性来保存和保存在我的数据库中的问题,如审计操作并在保存或更新任何bean之前保存数据库中的createdBy和modifiedBy,我发现在这个问题上的createdBy和modifiedBy有用的答案:Is it possible to use @PrePersist and @PreUpdate with eBean and Play! 2.0?是否有可能通过模型​​

这是我的课:

@MappedSuperclass 
public abstract class BaseModel extends Model { 

    @Id 
    @Column 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    public Integer id; 

    @Column 
    @CreatedTimestamp 
    public Timestamp createdDate; 

    @Column 
    @UpdatedTimestamp 
    public Timestamp updatedDate; 

    @Column 
    @Version 
    private long version=0; 

    @Column 
    public String updatedBy; 

    @Column 
    public String createdBy; 


    public BaseModel() { 
    } 

    @Override 
    public void save() { 
     boolean isInstanceOfAccount = this instanceof Account; 
     if (!isInstanceOfAccount) { 
      int userId = Integer.parseInt(session().get(ViewModelsConstants.User.USER_ID); 
      updatedBy = String.valueOf(userId); 
      // etc to 
     } 
     super.save(); 
    } 
} 

你可以注意到,我有重写保存方法(所以我会做到及时更新),所以当我打电话给我的bean时,如product.save()我保存id谁添加或更新谁。但我有一个类似的悬而未决问题Session inside Model in play framework

我也许可以添加方法有参数保存和调用超级喜欢:

public void save(int userId) { 
    boolean isInstanceOfAccount = this instanceof Account; 
    if (!isInstanceOfAccount) { 
     updatedBy = String.valueOf(userId); 
     // etc to 
    } 
    super.save(); 
} 

但我有已经存在的代码,我不想做修改在我所有的课,如果我想添加更多或删除我需要再重构


什么最酷的(最佳实践)解决方案,这个参数?

我现在简单的愚蠢问题:我如何能够从会话传递给模型,或者当我保存或更新它时如何保存参数(像登录时的userId)在模型中使用的地方?

回答

相关问题