4

I have a relation of type @ManyToMany with a @JoinTable association.

The thing is that the entity in the relation has its own table, but a couple of properties should go to the association table.

The A_C table is fine I think.

Adding the @SecondaryTable duplicate.

@Entity
@Table(name = "A")
@SecondaryTable(name = "A_C", pkJoinColumns = {
@PrimaryKeyJoinColumn(columnDefinition = "A_ID", name = "A_ID")})
class A {
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "A_B", joinColumns = {@JoinColumn(name = "A_ID")}, inverseJoinColumns = {@JoinColumn(name = "B_ID")})
    private List<B> bs = new ArrayList<B>();

    @Column(table = "A_B")
    private int b1;
}

@Entity
@Table(name = "B")
@SecondaryTable(name = "A_B", pkJoinColumns = {
@PrimaryKeyJoinColumn(columnDefinition = "B_ID", name = "B_ID")})
class B {
    @Column(table = "A_B")
    private int a1;

    @Column(table = "A_B")
    private int a2;

    @ManyToMany(mappedBy = "A_ID", fetch = FetchType.LAZY)
    private List<A> as = new ArrayList<A>();

}

This when saving A entities the B are duplicated in a way where:

A_ID    B_ID   a1     a2
   1       0    1      1    
   1       1    0      0

Where should be

A_ID    B_ID   a1    a2
   1       1    1     1

With @Embeddable won't work neither.

ssedano.

4

1 回答 1

0

A_B是 Table 的 Join-Table AB但您也将它用作B.

这行不通。如果将 a分配给多个s ,应该B.a1并且B.a2具有什么值?BA

有三种可能: * a1 和 a2 属于关联。在这种情况下,您需要一个关联类来存储该附加信息。* a1 和 a2 只属于 B:a1 和 a2 不应该在表中A_B *A是一个组成B:表A_B并且B应该被合并并且多对多关系被一对多关系取代。

如果没有属性的具体名称,很难判断它们是否真正属于。

于 2014-11-09T14:32:33.767 回答