2017-05-31 37 views
0

我指定@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; 
} 

任何幫助是極大的讚賞。

回答

0

另一側上使用的mappedBy完成雙向映射:

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

哇。那很簡單。謝謝! - 有沒有一個原因,我不能在'DamageReport'一側添加這個? – qbolt

+0

是的。在雙向映射中,必須始終存在一個所有者方(本例中爲DamageReport),而另一方(BoardMember)必須**僅引用所有者方(使用mappedBy =「boardMembers」)。 – adyjr