JSF: how to update the list after delete an item of that list
Posted
by
Harry Pham
on Stack Overflow
See other posts from Stack Overflow
or by Harry Pham
Published on 2010-12-31T11:37:15Z
Indexed on
2011/01/01
3:54 UTC
Read the original article
Hit count: 306
It will take a moment for me to explain this, so please stay with me. I have table COMMENT
that has OneToMany relationship with itself.
@Entity
public class Comment(){
...
@ManyToOne(optional=true, fetch=FetchType.LAZY)
@JoinColumn(name="REPLYTO_ID")
private Comment replyTo;
@OneToMany(mappedBy="replyTo", cascade=CascadeType.ALL)
private List<Comment> replies = new ArrayList<Comment>();
public void addReply(NewsFeed reply){
replies.add(reply);
reply.setReplyTo(this);
}
public void removeReply(NewsFeed reply){
replies.remove(reply);
}
}
So you can think like this. Each comment can have a List
of replies which are also type Comment
. Now it is very easy for me to delete the original comment and get the updated list back. All I need to do after delete is this.
allComments = myEJB.getAllComments(); //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 managed bean I have
//Before invoke this method, I have the value of originalFeed, and deletedFeed set.
//These original comments 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 comment 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();
}
}
Then inside my EJB scholarEJB, I have
public void removeReply(NewsFeed comment, NewsFeed reply){
comment = em.merge(comment);
comment.removeReply(reply);
em.persist(comment);
}
public void deleteFeeds(NewsFeed e){
e = em.find(NewsFeed.class, e.getId());
em.remove(e);
}
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. Please help
© Stack Overflow or respective owner