persistence@glassfish.java.net

PreRemove ConcurrentModificationException

From: Greg Ederer <greg_at_ergonosis.com>
Date: Wed, 13 Jun 2007 20:46:49 -0700

Oops! Accidentally hit send. Please ignore previous post.

I have a model containing three entity classes: Article, ArticleGroup
and ArticlePlacement. An ArticleGroup contains zero or more
ArticlePlacement objects. Each ArticlePlacement wraps an Article
(ArticlePlacement also has a start run date and an end run date, which
allows me to run an article in multiple groups during different periods).

So, in Article, I have:

  @OneToMany(mappedBy = "article", cascade=CascadeType.ALL)
  private List<ArticlePlacement> articlePlacements = new
ArrayList<ArticlePlacement>();

In ArticleGroup, I have:

  @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH})
  @OrderBy("position ASC")
  private List<ArticlePlacement> articlePlacements = new
ArrayList<ArticlePlacement>();

And, in ArticlePlacement, I have:

  @ManyToOne
  private Article article;
 
  @ManyToOne
  private ArticleGroup articleGroup;

  @PreRemove
  public void beforeRemove()
  {
    getArticleGroup().removeArticlePlacement(this);
    getArticle().removeArticlePlacement(this);
  }

When I EntityManager.remove() an Article, I get a
java.util.ConcurrentModificationException due to the
getArticle().removeArticlePlacement(this) in @PreRemove, above. But, if
I comment this out, I get a "violates foreign key" PSQLException.

Can someone tell me how to deal with this situation correctly?

Thanks!

Greg

-- 
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg_at_ergonosis.com
| 360.774.6848
|