mongo morphia basicDAO.save()基本上做到了
save(T entity)
Saves the entity; either inserting or overriding the existing document如果entity.id不为空,morphia会尝试覆盖现有文档,有没有办法让morphia抛出一个复制的键异常(就像mongodb那样)?
发布于 2017-08-23 16:27:24
最好的方法是检查文档是否已经存在。性能最好的是使用:
db.collection.find({_id: “myId”}, {_id: 1}).limit(1)然后检查是否为空。'findOne‘实际上会从数据库中加载文档,所以要避免这种情况。
https://stackoverflow.com/questions/45827520
复制相似问题