Hibernate : Data inconsistency

I have 2 entities Team and Player.

Team has  references of player and player has reference of a team. 

One-To-Many relationship from team to player ie Team object holds a collection of player object and can be saved irrespective of Player object pointing to a particular team. Hibernate allows to have this inconsistency. It delegates the logic on the programmer to maintain the data consistency.

Here you might save a team object with reference to players but when you call player.getTeam() you might get null pointer exception Smile

A way to avoid it could  be :

Team.addPlayertoList(Player player  ){

player.setTeam(this);
team.playerList.add(player);

}

Note : Handle both the operation in a single methods and make it atomic while adding and removing both from data design perspective.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s