Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3660924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:15:10+00:00 2026-05-19T01:15:10+00:00

It will take a moment for me to explain this, so please stay with

  • 0

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

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

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

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
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:15:11+00:00Added an answer on May 19, 2026 at 1:15 am

    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?


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

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have an XSLT that will take the app.config and render it into
This question will probably take a while to explain, and I'll need to provide
My boss has asked for a page that will not change to have two
I will take the example of stack overflow itself: Stack overflow displays icons using
This one will take some explaining. What I've done is create a specific custom
Anyone got a ready made function that will take an XML string and return
Is there a tool that will take you java beans (pojos) and automatically make
I am trying to write a nodejs server that will take a time input
I'm looking for a Java library that will can take a PDF and create
Outputting from a directions api I have a duration it will take the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.