gpt4 book ai didi

java - 这个多对多关系的 "Owning Side"是怎么确定的呢?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:58:58 25 4
gpt4 key购买 nike

我正在努力牢牢掌握拥有方的概念。无法从我在这里找到的任何问题中得到清晰的图片。基本上我正在阅读 Java EE JPA 教程。它们具有以下数据库模式,其中 PLAYERTEAM 具有多对多关系

enter image description here


也说明了

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

到目前为止非常简单。但是当 is 进入编码部分时,他们将 TEAM 设为关系的拥有方。

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;
}
}

问题

我对代码的理解没有问题。我无法处理的是:

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?

教程中也有说明。

"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."

话虽这么说:

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?

最佳答案

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

在双向场景中的多对多关系的情况下,可以任意选择关系的所有者,但考虑到您应该选择更有意义的实体首先检索或根据您的需要使用更多的实体目的。你只需要记住 Many 总是需要在 ManyToOne 场景中作为拥有方。

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

不,不会,因为关系是双向的。

  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?

是的,@JoinTable 必须像其他一些注释一样位于关系的所有者中。选择所有者后,您应该在该类中添加注释。

还要考虑在应用时需要的级联操作,以及关系的两边是否都需要。

关于java - 这个多对多关系的 "Owning Side"是怎么确定的呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com