mongo java 对象,Java MongoDB对象版本控制

小编典典

我们使用的是基本实体(我们在其中设置ID,创建+上次更改日期,…)。在此基础上,我们使用通用的持久性方法,该方法如下所示:

@Override

public ObjectId persist(E entity) {

delta(entity);

mongoDataStore.save(entity);

return entity.getId();

}

delta方法如下所示(我将尝试使其尽可能通用):

protected void delta(E newEntity) {

// If the entity is null or has no ID, it hasn't been persisted before,

// so there's no delta to calculate

if ((newEntity == null) || (newEntity.getId() == null)) {

return;

}

// Get the original entity

@SuppressWarnings("unchecked")

E oldEntity = (E) mongoDataStore.get(newEntity.getClass(), newEntity.getId());

// Ensure that the old entity isn't null

if (oldEntity == null) {

LOG.error("Tried to compare and persist null objects - this is not allowed");

return;

}

// Get the current user and ensure it is not null

String email = ...;

// Calculate the difference

// We need to fetch the fields from the parent entity as well as they

// are not automatically fetched

Field[] fields = ArrayUtils.addAll(newEntity.getClass().getDeclaredFields(),

BaseEntity.class.getDeclaredFields());

Object oldField = null;

Object newField = null;

StringBuilder delta = new StringBuilder();

for (Field field : fields) {

field.setAccessible(true); // We need to access private fields

try {

oldField = field.get(oldEntity);

newField = field.get(newEntity);

} catch (IllegalArgumentException e) {

LOG.error("Bad argument given");

e.printStackTrace();

} catch (IllegalAccessException e) {

LOG.error("Could not access the argument");

e.printStackTrace();

}

if ((oldField != newField)

&& (((oldField != null) && !oldField.equals(newField)) || ((newField != null) && !newField

.equals(oldField)))) {

delta.append(field.getName()).append(": [").append(oldField).append("] -> [")

.append(newField).append("] ");

}

}

// Persist the difference

if (delta.length() == 0) {

LOG.warn("The delta is empty - this should not happen");

} else {

DeltaEntity deltaEntity = new DeltaEntity(oldEntity.getClass().toString(),

oldEntity.getId(), oldEntity.getUuid(), email, delta.toString());

mongoDataStore.save(deltaEntity);

}

return;

}

我们的delta实体看起来像这样(没有getters + setters,toString,hashCode和equals):

@Entity(value = "delta", noClassnameStored = true)

public final class DeltaEntity extends BaseEntity {

private static final long serialVersionUID = -2770175650780701908L;

private String entityClass; // Do not call this className as Morphia will

// try to work some magic on this automatically

private ObjectId entityId;

private String entityUuid;

private String userEmail;

private String delta;

public DeltaEntity() {

super();

}

public DeltaEntity(final String entityClass, final ObjectId entityId, final String entityUuid,

final String userEmail, final String delta) {

this();

this.entityClass = entityClass;

this.entityId = entityId;

this.entityUuid = entityUuid;

this.userEmail = userEmail;

this.delta = delta;

}

希望这可以帮助您入门:-)

2020-10-18

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值