1

运行具有以下类的项目失败:

org.hibernate.MappingException: 外键 (FK9ex9b0501jh788ukls6wuglix:a_bc [bc_id])) 的列数必须与引用的主键 (bc [b_id,c_id]) 相同

@Entity
public class A {
    @Id
    private Long id;

    @ManyToMany
    @JoinTable(name = "A_BC",
    joinColumns = @JoinColumn(name = "A_ID"),
    inverseJoinColumns = @JoinColumn(name = "BC_ID"))
    private Set<BC> set;
}

@Entity
public class B {
    @Id
    private Long id;

    @ManyToMany
    @JoinTable(name = "BC",
    joinColumns = @JoinColumn(name = "B_ID"),
    inverseJoinColumns = @JoinColumn(name = "C_ID"))
    private Set<C> set;
}

@Entity
public class C {
    @Id
    private Long id;

    @ManyToMany
    @JoinTable(name = "BC",
    joinColumns = @JoinColumn(name = "C_ID"),
    inverseJoinColumns = @JoinColumn(name = "B_ID"))
    private Set<B> set;
}

@Entity
public class BC {
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name = "B_ID")
    private B b;

    @ManyToOne
    @JoinColumn(name = "C_ID")
    private C c;

    @ManyToMany
    @JoinTable(name = "A_BC",
    joinColumns = @JoinColumn(name = "BC_ID"),
    inverseJoinColumns = @JoinColumn(name = "A_ID"))
    private Set<A> set;
}

@Entity
public class A_BC {
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name = "A_ID")
    private A a;

    @ManyToOne
    @JoinColumn(name = "BC_ID")
    private BC bc;
}

实体 B、C 和 BC 按预期工作。B 和 C 使用 BC 作为连接表相互连接。

当引入实体 A 并将其与 BC 关联时,就会出现问题。BC 充当连接表,但它也是另一个连接表 (A_BC) 的一部分,我认为这是导致错误的原因,但我不明白为什么或如何修复它。

4

1 回答 1

0

问题是您的一侧有@OneToMany,另一侧有@ManyToMany。

于 2019-07-31T10:26:59.747 回答