Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the "Owning Side" of this many-to-many relationship determined?

I'm trying to get a firm grasp on the concept of Owning-side. Couldn't really get a clear picture from any question i found here. Basically I'm going through the Java EE JPA tutorial. They have the following database schema, where PLAYER and TEAM have a many-to-many relationship

enter image description here


Also stated

  • A player can be on many teams.
  • A team can have many players.
  • There is a many-to-many relationship between PLAYER and TEAM.

Pretty straight forward so far. But when is gets to the coding part, they make the TEAM the owning side of the relationship.

public class Team {
    private Collection<Player> players;

    @ManyToMany
    @JoinTable(
            name = "PERSITENCE_ROSTER_TEAM_PLAYER",
            joinColumns = @JoinColumn(name = "TEAM_ID", referencedColumnName = "ID"),
            inverseJoinColumns = @JoinColumn(name = "PLAYER_ID", referencedColumnName = "ID")
    )
    public Collection<Player> getPlayers() {
        return players;
    }
}

public class Player {
    private Collection<Team> teams;

    @ManyToMany(mappedBy = "players")
    public Collection<Team> getTeams() {
        return teams;
    }
}

Question(s)

I have no problem understanding the code. What I can't get a handle on is:

1. How is it determined that TEAM is the owning side?

2. Would it make any difference if PLAYER was made the owning side instead, in this scenario?

Also stated from the tutorial.

"The entity that specifies the @JoinTable is the owner of the relationship, so the TEAM entity is the owner of the relationship with the PLAYER entity."

That being said:

3. Would the above statement make my second question true? Meaning that there is no determining factor besides which one you decide to make the owning side, with the @JoinTable annotation?

like image 834
Paul Samsotha Avatar asked Feb 24 '14 10:02

Paul Samsotha


People also ask

What is the owning side?

In a One-to-Many/Many-to-One relationship, the owning side is usually defined on the many side of the relationship. It's usually the side that owns the foreign key.

Who is the owner of a relationship in a bidirectional relationship?

The many side is always the owning side of the relationship. For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key. For many-to-many bidirectional relationships, either side may be the owning side.

Who is the owner of relationship?

What are Relationship Owners? Relationship Owners allow you to assign your teammates to a journalist to help track and manage your team's most important relationships. The teammates who have been assigned on a journalist profile page are Relationship Owners.

What is association differentiate between unidirectional and bidirectional association?

The direction of a relationship can be either bidirectional or unidirectional. A bidirectional relationship has both an owning side and an inverse side. A unidirectional relationship has only an owning side.


Video Answer


1 Answers

  1. How is it determined that TEAM is the owning side?

In the case of ManytoMany relationships in bidirectional scenario the Owner of the relationship can be selected arbitrarily, but having in mind the purpose you should select the entity that makes more sense to retrieve first or the one that is more used according to your purpose. You only need to remember that Many always need to be the owning side in ManyToOne scenarios.

  1. Would it make any difference if PLAYER was made the owning side instead, in this scenario?

No, it wouldn´t as the relationship is bidirectional.

  1. Would the above statement make my second question true? Meaning that there is no determining factor besides which one you decide to make the owning side, with the @JoinTable annotation?

Yes, @JoinTable must to be in the owner in the relationship as some other annotations. Once you select the owner you should add the annotation in that class.

Consider also the Cascade operation you need to have if apply, and if there are need to be in both side of the relationship.

like image 125
Koitoer Avatar answered Oct 26 '22 02:10

Koitoer