是否可以通过模型从数据库中的会话中保存值
Is it possible to save value from session in DB through Model
我在获取 userId 和其他属性以保存和保存在我的数据库中时遇到问题,例如审计操作并在保存或更新任何 bean 之前将 createdBy 和 modifiedBy 保存在数据库中,我在此找到了有关 createdBy 和 modifiedBy 的有用答案问题:Is it possible to use @PrePersist and @PreUpdate with eBean and Play! 2.0?:
这是我的 class :
@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();
}
}
你可以注意到我已经重写了保存方法(所以我会做更新)所以当我像 product.save()
那样调用我的 bean 时,我会保存 id
给谁添加或更新。但我有一个与 Session inside Model in play framework、
类似的未解决问题
我也许可以添加具有参数的方法来保存和调用 super,例如:
public void save(int userId) {
boolean isInstanceOfAccount = this instanceof Account;
if (!isInstanceOfAccount) {
updatedBy = String.valueOf(userId );
// etc to
}
super.save();
}
但是我已经有了代码,我不想在我的所有 class 中进行修改,如果我想添加更多或删除参数,我需要重构
最酷(最佳实践)的解决方案是什么?
我现在的简单愚蠢问题:我如何从会话传递到模型,或者我如何在保存或更新模型时将参数(如他登录时的 userId)保存在某个地方以在模型中使用?
我认为这个问题太老了,我在体验时找到了它的解决方案,但如果你正在寻找答案,你可以在这里看到我最新的答案和问题:
它存储没有 cookie 的令牌,或者您可以使用 cookie 并从控制器读取它。
我在获取 userId 和其他属性以保存和保存在我的数据库中时遇到问题,例如审计操作并在保存或更新任何 bean 之前将 createdBy 和 modifiedBy 保存在数据库中,我在此找到了有关 createdBy 和 modifiedBy 的有用答案问题:Is it possible to use @PrePersist and @PreUpdate with eBean and Play! 2.0?:
这是我的 class :
@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();
}
}
你可以注意到我已经重写了保存方法(所以我会做更新)所以当我像 product.save()
那样调用我的 bean 时,我会保存 id
给谁添加或更新。但我有一个与 Session inside Model in play framework、
我也许可以添加具有参数的方法来保存和调用 super,例如:
public void save(int userId) {
boolean isInstanceOfAccount = this instanceof Account;
if (!isInstanceOfAccount) {
updatedBy = String.valueOf(userId );
// etc to
}
super.save();
}
但是我已经有了代码,我不想在我的所有 class 中进行修改,如果我想添加更多或删除参数,我需要重构
最酷(最佳实践)的解决方案是什么?
我现在的简单愚蠢问题:我如何从会话传递到模型,或者我如何在保存或更新模型时将参数(如他登录时的 userId)保存在某个地方以在模型中使用?
我认为这个问题太老了,我在体验时找到了它的解决方案,但如果你正在寻找答案,你可以在这里看到我最新的答案和问题:
它存储没有 cookie 的令牌,或者您可以使用 cookie 并从控制器读取它。