Saturday, August 27, 2011

ManyToOne Update When Child Delete JPA

Using JPA, if you have 2 entities, one with a many-to-one relationship, you probably need to update the "many" side when the other side is deleted. The only method I could find for this was to set all the entities on the "many" side in the method to delete the child.

Example:

Player {

@ManyToOne
Team team;

}

When the Team is deleted, an update statement needs to be run to set all Team to null for all players on that team.