删除列表中的项目后如何更新列表 [英] How to update the list after delete an item of that list

查看:80
本文介绍了删除列表中的项目后如何更新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要花一点时间来解释这一点,所以请和我在一起.我有与自己具有OneToMany关系的表NewsFeed.

It will take a moment for me to explain this, so please stay with me. I have table NewsFeed that has OneToMany relationship with itself.

@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.现在,我很容易删除原始的供稿并获取更新后的列表.删除后我需要做的就是这个.

So you can think like this. Each feed can have a List of replies which are also type NewsFeed. Now it is very easy for me to delete the original feed and get the updated list back. All I need to do after delete is this.

feeds = scholarEJB.findAllFeed();  //This will query the db and return updated list

但是在尝试删除replies并取回更新的列表时遇到问题.因此,这就是删除replies的方法.在我的JSF托管bean中

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 ScholarEJB内部,我有

Then inside my EJB scholarEJB, I have

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
}

当我离开时,实体(答复)已从数据库中正确删除,但是在feeds列表中,该reply的引用仍然存在.直到我注销并重新登录后,答复才会消失.

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.

推荐答案

您所写的内容是正确的,重新查询应该可以工作(实际上是通常的工作方式),因此问题必须出在其他地方.例如,如果您通过除remove方法以外的任何其他方式删除实体,则该实体可能仍位于第二级缓存中.还是因为一些小错误而根本没有发生重新查询?

What you have written is correct, requerying should work (and in factis how it's usually done), so the problem must be somewhere else. For example if you delete the entity by any means other than the remove method, it may still be in the 2nd level cache. Or maybe the requerying does not happen at all because of some small mistake?

更新:阅读了原始问题的新版本(包括所有新资源和有关Eclipselink的说明)后,我做了一点测试,这确实是第二级缓存问题.有点奇怪,可能是错误或未指定的特殊情况.要解决此问题,请从缓存中逐出注释:

Update: After reading the new version of the original question (with all the new sources and clarification about Eclipselink) I made a little test, and it is - indeed - a second level cache problem. A bit strange, and possibly either a bug or an underspecified corner case. To fix it, evict comments from cache:

// this goes after em.remove(e)
em.getEntityManagerFactory().getCache().evict(Comment.class);

您还可以尝试仅驱逐已删除实体的父级(存在逐出的超载版本).

You could also try evicting the parent of the removed entity only (there is an overloaded version of evict).

嗯.也许有更多街头信誉(BalusC?:-)的人应该更改此帖子上的标签.事实证明,它与JSF没有任何关系.

Uhmpf. Maybe someone with more street cred (BalusC? :-) should change tags on this post. Turned out it has nothinh to do with JSF.

这篇关于删除列表中的项目后如何更新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆