简体   繁体   English

使用附加关系表来休眠一对多映射

[英]Hibernate one to many mapping with additional relation table

I want to have two tables with one to many relation that are linked by third table. 我希望有两个表由一对多关系,由第三个表链接。 How can I approach this? 我怎么处理这个? I want to create exactly the same thing as in this tutorial but using one-to-many instead of many-to-many with unique="true" 我想创建与本教程中完全相同的东西,但是使用一对多而不是多对多,其中unique =“true”

In Hibernate when you use a @OneToMany annotation without stating a @JoinTable or @JoinColumn a third table will be automatically created to map the relationship, so no worries in just switching the @ManyToMany by @OneToMany, as long you relationship will follow the rules of the annotation. 在Hibernate中,当您使用@O​​neToMany注释而不声明@JoinTable或@JoinColumn时,将自动创建第三个表以映射关系,因此不必担心仅仅通过@OneToMany切换@ManyToMany,只要您的关系遵循规则注释。

However, you can try some explicit mapping, so you will be able to control even de column names that will be created by the @OneToMany annotation. 但是,您可以尝试一些显式映射,因此您将能够控制将由@OneToMany批注创建的de列名称。

Try something like this: 尝试这样的事情:

public class TestClass1 {

    @OneToMany
    @JoinTable(name = "ADDITIONAL TABLE NAME", joinColumns = {
        @JoinColumn(name = "TESTCLASS1_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "TESTCLASS2_ID")})
    private List<TestClass2> listTestClass2;
}

Good luck! 祝好运!

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

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