I managed to get this working by adding the following to Article:
@PreRemove
public void beforeRemove()
{
List<ArticlePlacement> articlePlacements = getArticlePlacements();
for(ArticlePlacement ap : articlePlacements)
{
ap.setArticle(null);
}
}
And in ArticlePlacement:
@PreRemove
public void beforeRemove()
{
getArticleGroup().removeArticlePlacement(this);
if(getArticle() != null)
{
getArticle().removeArticlePlacement(this);
}
}
I have a feeling this is not the best way to handle this (and, I'm
guessing that I'll feel like a dummy when I find out the correct way to
do this, because it will be pretty obvious).
Any advice welcome.
Cheers,
Greg
Greg Ederer wrote:
> 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
|