2012-05-04 145 views

回答

2

假設博客帖子實體可以有多個評論,但每個評論只屬於一個博客帖子。

首先,您需要刪除參考:

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 
} 

然後你就可以刪除實體本身:

mongoDataStore.delete(comment);