简体   繁体   English

使用Hibernate将许多表连接到一个[多对多和额外的列]

[英]Join many tables to one [Many-to-many and with extra columns] using Hibernate

Could you help me please, using hibernate and annotations I want to join 4 tables (prof, salle , groupe, cours) with another one (creneau). 您能用冬眠和注释来帮助我吗,我想将4个表(prof,salle,groupe,cours)与另一个表(creneau)连接起来。 the 4 of them have a many-to-many relationship with the 5th table. 其中的4个与第5个表具有多对多关系。

I googled it, I know that with many-to-many relationship (I found only the case of 2 tables ) will create a link table to join them, it contains their keys (in my case there will be extra columns also) 我用谷歌搜索,我知道具有多对多关系(我发现只有2个表的情况)将创建一个链接表来连接它们,它包含它们的键(在我的情况下,还将有额外的列)

This is the best link I found Join Tutorial . 这是我找到的最佳链接Join Tutorial The solution that I found more appropriate to the problem would be to repeat that tutorial for the 4 tables (prof-creneau)/(salle/creneau) .... and with the same link table. 我发现更适合该问题的解决方案是针对4个表(prof-creneau)/(salle / creneau)....并使用相同的链接表重复该教程。

Is it the best way to achieve it (for me it seems repetitive) ? 这是实现它的最佳方法(对我来说似乎是重复的)?

Classic @ManyToMany relationship refers between TWO entities. 经典@ManyToMany关系是指两个实体之间的关系。

If you want many-to-many table relationship, with extra column you need join table. 如果要建立多对多表关系,则需要额外的列才能连接表。

Here is useful example: 这是有用的示例:

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

As I mentioned in my comment on your answers, I'm not 100% clear, but I'm guessing you have four distinct many-to-many relationships: 正如我在对您的答案的评论中提到的那样,我不是100%清楚,但是我猜您有四个截然不同的多对多关系:

  1. prof-creneau 克里尼奥教授
  2. salle-creneau 萨勒克雷瑙
  3. groupe-creneau 克雷诺
  4. cours-creneau 库尔斯·克雷瑙

If that is the case, then your guess is correct. 如果真是这样,那么您的猜测是正确的。 The best way to achieve it is to have four separate many-to-many relationships. 实现它的最佳方法是拥有四个单独的多对多关系。 This means you will have four join tables (one for each relationship, each one has two columns; a foreign key for creneau and a foreign key for the other table in the relationship). 这意味着您将有四个联接表(每个关系一个,每个关系有两列;该关系中的一个外键用于creneau,另一个表的外键)。

In Hibernate you would specify all of these mappings separately. 在Hibernate中,您将分别指定所有这些映射。

Your DAO classes could look something like: 您的DAO类可能类似于:

public class Prof {
    private Set<Creneau> creneaus = ...;
}

public class Salle {
    private Set<Creneau> creneaus = ...;
}

public class Groupe {
    private Set<Creneau> creneaus = ...;
}

public class Cours {
    private Set<Creneau> creneaus = ...;
}

public class Creneau {
    private Set<Prof> profs = ...;
    private Set<Salle> salles = ...;
    private Set<Groupe> groupes = ...;
    private Set<Cours> courss = ...;
}

It may seem repetitive, but you do have four separate relationships here. 它似乎是重复的,但是您在这里确实有四个独立的关系。

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

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