Morphia - remove a reference object

十年热恋 提交于 2019-12-11 19:47:16

问题


Lets say a Blog class with Comment Object as reference. Comment Object has Id, Comment Date, Comment. (Reference) NOT EMBEDDED.

How do I remove a comment?


回答1:


Assuming a blog post entity can have multiple comments, but each comment belongs to exactly one blog post.

First you'll need to remove the reference:

BlogPostEntity blog = mongoDataStore.find(BlogEntity.class)
    .field("comments")
    .hasThisElement(new Key<CommentEntity>(CommentEntity.class, comment.getId()))
    .get();
if (blog != null) {
    blog.removeComment(comment); // Assuming you have a remove method for that, otherwise use the setter
    persist(blog); // Assuming you have a generic persist method
}

Then you can remove the entity itself:

mongoDataStore.delete(comment);


来源:https://stackoverflow.com/questions/10443372/morphia-remove-a-reference-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!