“拥有方"的情况如何?确定的这种多对多关系? [英] How is the "Owning Side" of this many-to-many relationship determined?

查看:54
本文介绍了“拥有方"的情况如何?确定的这种多对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图牢牢把握所有权方面的概念.我在这里发现的任何问题都无法真正获得清晰的画面.基本上,我正在阅读Java EE JPA教程.它们具有以下数据库架构,其中PLAYERTEAM具有多对多关系

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

也已说明

  • 一个玩家可以参加很多团队.
  • 一个团队可以有很多球员.
  • PLAYERTEAM之间存在多对多关系.
  • A player can be on many teams.
  • A team can have many players.
  • There is a many-to-many relationship between PLAYER and TEAM.

到目前为止非常简单.但是当到达编码部分时,它们使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.如何确定TEAM是拥有方?

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

2.在这种情况下,如果将PLAYER设为拥有方,会有所不同吗?

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

也在本教程中指出.

"指定@JoinTable的实体是该所有者 关系,因此TEAM实体是与PLAYER实体的关系的所有者."

"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.上面的陈述会使我的第二个问题成为事实吗?意思是说,除了决定是拥有所有权的一方之外,没有其他决定性因素了,并带有@JoinTable注释?

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. 如何确定TEAM是拥有方?

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

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. 在这种情况下,如果将PLAYER设为拥有方,会有所不同吗?

不,这不是双向关系.

  1. 以上陈述是否会使我的第二个问题正确?意思是说,除了您决定要决定的因素之外,没有其他决定因素. 使用@JoinTable批注来拥有所有权?
  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必须和其他一些注释一样位于关系的所有者中.选择所有者后,您应该在该类中添加注释.

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.

这篇关于“拥有方"的情况如何?确定的这种多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆