English 中文(简体)
在删除该清单的一个项目后如何更新该清单
原标题:How to update the list after delete an item of that list
  • 时间:2010-12-31 11:37:15
  •  标签:
  • java
  • jpa

我需要一个时间来解释这一点,因此请与我一道发言。 我有以下表格:,与自己有“一对一”关系。

@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.

最佳回答

你所写的是正确无误的,重新排列顺序应当奏效(事实上是它通常如何做),因此,问题必须放在其他地方。 例如,如果你以除去方法以外的任何手段删除该实体,该实体可能仍然处于第二位。 或者由于一些小的错误,重新排序根本不会发生吗?


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);

您还可以尝试将被除名实体的母子驱逐(即超负荷的切除)。

Uhmpf. 或许还有人流落街头(BalusC?:-) 应改变这一职位上的主角。 改称与共同财产法无关。

问题回答

删除物体时,你必须删除所有提及该物体之处,然后再删除该物体。 如果你不这样做,你就会出现一种制约因素错误,或者在您的持久性背景下,或者在第2级的海滩上留下了数据。

在删除Fed时,首先从答复中删除。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签