persistence@glassfish.java.net

cascading removals only when last foreign key is removed

From: John O'Conner <John.Oconner_at_Sun.COM>
Date: Mon, 23 Apr 2007 16:02:13 -0700

I have two entities:

Player
Team


Player is in a M:1 bidirectional relationship with Team. Player is the
owner.

Player's team property has the @ManyToOne(cascade=CascadeType.ALL)
annotation.

Now when I remove a single Player, the Team is also removed. Obviously
that's wrong when other players belong to that team, and I guess I need
to remove the cascade attribute. However, I would like the associated
team removed when the last player is removed. Is there a slick way to do
this without performing my checks to see whether the team_id is still in
PLAYER?

The tables look like this:

PLAYER
id, name, jerseynumber, team_id

TEAM
id, name, league


Regards,
John O'Conner