简体   繁体   English

是否可以在Hibernate中映射表的两列而不是两个表

[英]Is it possible to map two columns of table instead of two tables in Hibernate

I am having a situation where 1 table is having 2 columns and they have the many to many relation between them. 我遇到的情况是1个表有2列,它们之间有多对多关系。 Let me explain with example I have a table named 让我举例说明一下,我有一个名为

StudentCC 学生CC

StudentId | CoachingCenterId
    1              C1
    1              C2
    2              C1
    2              C2

Now this is a manyToManyRelation between columns of 1 table. 现在,这是1个表的列之间的manyToManyRelation。 I searched internet and didn't find anything related to this.Every suggestion is specific to at least two tables. 我搜索了互联网,但没有找到与此相关的任何内容。每个建议至少都针对两张桌子。 What I understand is that it can be done in ManyToMany as well as OneToMany and ManyToOne relation mapping. 我了解的是,它可以在ManyToMany以及OneToMany和ManyToOne关系映射中完成。 But don't understand how. 但是不知道如何。

I tried to build a ManyToMany relation: Please suggest if its true. 我试图建立一个ManyToMany关系:请提出是否正确。 And if I have to use this or 2 OneToMany and ManyToOne relation approach. 如果我必须使用这种或2个OneToMany和ManyToOne关系方法。

@ManyToMany(MappedBy="AnyThing" cascade=CascadeType.ALL)
@JoinTable(name="StudentCC" JoinColumns={@JoinColumn(name="StudentId")},
inverseJoinColumns{@JoinColumn(name="CoachingCenterId")})
Private Set<StudentCC> rel = new HashSet<StudentCC>();

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="StudentCC" JoinColumns=@JoinColumn(name="CoachingCenterId")},
inverseJoinColumns{@JoinColumn(name="StudentId")})
Private Set<StudentCC> rel = new HashSet<StudentCC>();

Please advice . 请指教 。 Thanks in advance. 提前致谢。

I belive you cannot have the JointTable(name="StudentCC") as many to many can not group to StudentCC nor coachingCenterID, you will need a new table to hold the many to many between Student and coachingCenter, such as StudentCoachingCenter. 我相信您不能拥有JointTable(name =“ StudentCC”),因为无法将多对多分组到StudentCC或coachingCenterID,您将需要一个新表来容纳Student和coachingCenter之间的多对多内容,例如StudentCoachingCenter。

In Student class you want the set with CoachingCenters 在“学生”课程中,您需要使用CoachingCenters进行设置

 @ManyToMany
    @JoinTable(name="StudentCoachingCenter", 
    joinColumns = {@JoinColumn(name="StudentCC")}, inverseJoinColumns ={@JoinColumn(name="coachingCenterID")})
   private Set<CoachingCenter> coachingCenters;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM