与连接表的一对多关系

时间:2021-04-25 09:58:14

I have a one-to-many relationship modeled using join table:

我有一个使用连接表建模的一对多关系:

create table t1 (id int primary key, name varchar(10) /*...*/);
create table t2 (id int primary key, name varchar(10) /*...*/);
create table t1_t2 (t1_id int, t2_id int, primary key (t1, t2));

The tables are supposed to model the relationship of one t1 to many t2. What is the right way to model these tables using JPA?

这些表应该模拟一个t1到多个t2的关系。使用JPA对这些表建模的正确方法是什么?

1 个解决方案

#1


50  

The typical table for one T1 to many T2 is to have a foreign key on T2 pointing toward T1. The T1_T2 table is usually not needed.

一个T1到多个T2的典型表是在T2上指向T1的外键。通常不需要T1_T2表。

The JPA structure would then be a One-To-Many, possibly two-way.

然后,JPA结构将是一对多,可能是双向的。


There could be some arrangements, to make the structure you describe work. You could change T1_T2:

可能会有一些安排,以使您描述的结构起作用。你可以改变T1_T2:

  • add a unique constraint on T2 (so that only one T2 is allowed)
  • 在T2上添加一个唯一约束(这样只允许一个T2)

Is that really what you want?

这真的是你想要的吗?

Edited: yes, it is what you want ;-)

编辑:是的,这是你想要的;-)

I doubt you may find many examples on the net. I have no proved solution, but I would try something along these lines:

我怀疑你可能在网上找到很多例子。我没有经过证明的解决方案,但我会尝试这些方法:

In Hibernate annotation reference documentation, see "2.2.5.3.2.3. Unidirectional with join table" to get the idea. It looks like:

在Hibernate注释参考文档中,请参阅“2.2.5.3.2.3。带有连接表的单向”以获得想法。看起来像:

    @Entity
    public class Trainer {
        @OneToMany
        @JoinTable(
            name="TrainedMonkeys",
            joinColumns = @JoinColumn( name="trainer_id"),
            inverseJoinColumns = @JoinColumn( name="monkey_id")
        )
        public Set<Monkey> getTrainedMonkeys() {
        ...
    }

#1


50  

The typical table for one T1 to many T2 is to have a foreign key on T2 pointing toward T1. The T1_T2 table is usually not needed.

一个T1到多个T2的典型表是在T2上指向T1的外键。通常不需要T1_T2表。

The JPA structure would then be a One-To-Many, possibly two-way.

然后,JPA结构将是一对多,可能是双向的。


There could be some arrangements, to make the structure you describe work. You could change T1_T2:

可能会有一些安排,以使您描述的结构起作用。你可以改变T1_T2:

  • add a unique constraint on T2 (so that only one T2 is allowed)
  • 在T2上添加一个唯一约束(这样只允许一个T2)

Is that really what you want?

这真的是你想要的吗?

Edited: yes, it is what you want ;-)

编辑:是的,这是你想要的;-)

I doubt you may find many examples on the net. I have no proved solution, but I would try something along these lines:

我怀疑你可能在网上找到很多例子。我没有经过证明的解决方案,但我会尝试这些方法:

In Hibernate annotation reference documentation, see "2.2.5.3.2.3. Unidirectional with join table" to get the idea. It looks like:

在Hibernate注释参考文档中,请参阅“2.2.5.3.2.3。带有连接表的单向”以获得想法。看起来像:

    @Entity
    public class Trainer {
        @OneToMany
        @JoinTable(
            name="TrainedMonkeys",
            joinColumns = @JoinColumn( name="trainer_id"),
            inverseJoinColumns = @JoinColumn( name="monkey_id")
        )
        public Set<Monkey> getTrainedMonkeys() {
        ...
    }