当我指定一个时,Hibernate会生成它自己的连接表

时间:2017-05-31 20:08:37

标签: java hibernate jpa

我为@JoinTable指定了@ManyToMany,以便我可以为表,列和外键指定名称。

Hibernate似乎正在正确地生成我的,但它仍然试图生成它自己的。

这是有问题@ManyToMany的实体。仅包含来自实体的相关代码。

@Entity
@Table(name = "DAMAGE")
public class DamageReport {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "DAMAGE_ID_GENERATOR")
    @SequenceGenerator(name = "DAMAGE_ID_GENERATOR", sequenceName = "DAMAGE_ID_SQ")
    @Column(name = "DAMAGE_ID")
    private Long id;

    //...Bunch of other fields

    @ManyToMany
    @JoinTable(name = "BOARD_MEMBER_DAMAGE_REPORTS", joinColumns = {
            @JoinColumn(name = "DAMAGE_REPORT_ID") }, inverseJoinColumns = {
                    @JoinColumn(name = "BOARD_MEMBER_ID") }, foreignKey = @ForeignKey(name = "DAMAGE_REPORT_ID_FK"), inverseForeignKey = @ForeignKey(name = "BOARD_MEMBER_ID_FK"))
    private Set<BoardMember> boardMembers;
}

以下是@ManyToMany中的其他实体。仅包含来自实体的相关代码。

@Entity
@Table(name = "BOARD_MEMBER")
public class BoardMember {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "BOARD_MEMBER_ID_GENERATOR")
    @SequenceGenerator(name = "BOARD_MEMBER_ID_GENERATOR", sequenceName = "BOARD_MEMBER_ID_SQ")
    @Column(name = "BOARD_MEMBER_ID")
    private Long id;

    @ManyToMany
    public Set<DamageReport> damageReports;
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在另一侧使用mappedBy来完成双向映射:

@ManyToMany(mappedBy = "boardMembers")
public Set<DamageReport> damageReports;