我需要一个时间来解释这一点,因此请与我一道发言。 我有以下表格:
@Entity
public class NewsFeed(){
...
@ManyToOne(optional=true, fetch=FetchType.LAZY)
@JoinColumn(name="REPLYTO_ID")
private NewsFeed replyTo;
@OneToMany(mappedBy="replyTo", cascade=CascadeType.ALL)
private List<NewsFeed> replies = new ArrayList<NewsFeed>();
public void addReply(NewsFeed reply){
replies.add(reply);
reply.setReplyTo(this);
}
public void removeReply(NewsFeed reply){
replies.remove(reply);
}
}
因此,你可以这样想。 每一饲料都有一份<代码>List的答复,这些答复也属于NewsFeed
。 现在,我很容易删除原始材料,重新编制更新清单。 在删除之后,我都需要这样做。
feeds = scholarEJB.findAllFeed(); //This will query the db and return updated list
But I am having problem when trying to delete replies
and getting the updated list back. So here is how I delete the replies
. Inside my JSF managed bean I have
//Before invoke this method, I have the value of originalFeed, and deletedFeed set.
//These original feeds are display inside a p:dataTable X, and the replies are
//displayed inside p:dataTable Y which is inside X. So when I click the delete button
//I know which feed I want to delete, and if it is the replies, I will know
//which one is its original post
public void deleteFeed(){
if(this.deletedFeed != null){
scholarEJB.deleteFeeds(this.deletedFeed);
if(this.originalFeed != null){
//Since the originalFeed is not null, this is the `replies`
//that I want to delete
scholarEJB.removeReply(this.originalFeed, this.deletedFeed);
}
feeds = scholarEJB.findAllFeed();
}
}
然后,在我的EJB学者EJB中,我就在我的EJEJB中。
public void removeReply(NewsFeed feed, NewsFeed reply){
feed = em.merge(feed);
comment.removeReply(reply);
em.persist(comment);
}
public void deleteFeeds(NewsFeed e){
e = em.find(NewsFeed.class, e.getId());
em.remove(e);
em.getEntityManagerFactory().getCache().evict(NewsFeed.class); //Like fdreger suggested
}
When I get out, the entity (the reply) get correctly removed from the database, but inside the feeds
List, reference of that reply
still there. It only until I log out and log back in that the reply disappear.