问题
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