【发布时间】:2015-11-26 08:58:20
【问题描述】:
我有 MongoDB 的 Spring Web 应用程序。目前我总是从数据库中永久删除数据。
@Repository
public class SessionRepository extends CrudRepository implements SessionService {
...
@Override
public void insert(Session session) {
saveRoom(session);
getTemplate().insert(session);
}
@Override
public void delete(Session session) {
getTemplate().remove(session);
}
...
}
将其更改为软删除的好方法是什么?
----------------- 编辑 1 -------------------
我现在明白我应该做什么的逻辑了,谢谢 Sarath Nair。但我不确定如何在 Spring 中实现这一点。我有一个 Session 对象:
@Document(collection = "session")
public class Session {
@Id
private String id;
private Date startDate;
private Date endDate;
//I just put this here
private boolean deleted = false;
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
...
}
我希望字段boolean isDeleted 出现在数据库中,但我不希望通过网络服务发送该信息。
@Transient 不好,因为这样该字段就不会出现在数据库中,也不会出现在 HTTP 响应中。现在我在我的 HTTP 响应中发送 deleted: false。
我应该如何编辑我的 Session 类?
【问题讨论】:
-
找到解决方案。由于 Spring 在内部使用 Jackson,@JsonIgnore 使得字段不会出现在响应中