2015-01-06 114 views
0

我一直在面對映射異常,我簡直不明白。 這裏是個例外:Hibernate映射異常與連接表

Caused by: org.hibernate.MappingException: Foreign key (FKCA2E919A5E0752D:cim10_indicationgroup_indicator [indicatorId])) must have same number of columns as the referenced primary key (cim10_indicationgroup [indicationGroupId,cim10Id]) 

的表如下(通知已經是關於PK差異):

desc cim10_indicationgroup_indicator; 
+-------------------------+---------+------+-----+---------+-------+ 
| Field     | Type | Null | Key | Default | Extra | 
+-------------------------+---------+------+-----+---------+-------+ 
| cim10_indicationgroupId | int(11) | NO | PRI | NULL |  | 
| indicatorId    | int(11) | NO | PRI | NULL |  | 
+-------------------------+---------+------+-----+---------+-------+ 

desc cim10_indicationgroup; 
+-------------------------+---------+------+-----+---------+-------+ 
| Field     | Type | Null | Key | Default | Extra | 
+-------------------------+---------+------+-----+---------+-------+ 
| cim10_indicationGroupId | int(11) | NO | PRI | NULL |  | 
| indicationGroupId  | int(11) | NO | MUL | NULL |  | 
| cim10Id     | int(11) | NO | MUL | NULL |  | 
+-------------------------+---------+------+-----+---------+-------+ 

desc indicator; 
+-------------+--------------+------+-----+---------+-------+ 
| Field  | Type   | Null | Key | Default | Extra | 
+-------------+--------------+------+-----+---------+-------+ 
| indicatorId | int(11)  | NO | PRI | NULL |  | 
| indicator | varchar(255) | NO |  | NULL |  | 
+-------------+--------------+------+-----+---------+-------+ 

在休眠方面,實體立場如下:

@Entity 
@Table(name = "cim10_indicationgroup") 
@Immutable 
public class Cim10IndicationGroupCouple { 

    @Id 
    @Column(name = "cim10_indicationGroupId") 
    private Integer cim10IndicationGroupId; 

    @Column(name = "indicationGroupId") 
    private Integer indicationGroupId; 

    @Column(name = "cim10Id") 
    private Integer cim10Id; 

    @ManyToMany 
    @JoinTable(
     name = "cim10_indicationgroup_indicator", 
     joinColumns = @JoinColumn(name = "indicatorId") 
    ) 
    private Set<Indicator> indicators; 

    // getters, setters, equals/hashcode 

並參考實體:

@Entity 
@Table(name = "indicator") 
@Immutable 
public class Indicator { 

    @Id 
    @Column(name = "indicatorId") 
    private Integer id; 

    @Column(name = "indicator") 
    private String indicator; 

    // getters, setters, equals/hashcode 

作爲獎勵,來這裏的元數據,從加入問題表:

SHOW KEYS FROM cim10_indicationgroup\G 
*************************** 1. row *************************** 
     Table: cim10_indicationgroup 
    Non_unique: 0 
    Key_name: PRIMARY 
Seq_in_index: 1 
Column_name: cim10_indicationGroupId 
    Collation: A 
Cardinality: 10137 
    Sub_part: NULL 
     Packed: NULL 
     Null: 
    Index_type: BTREE 
    Comment: 
*************************** 2. row *************************** 
     Table: cim10_indicationgroup 
    Non_unique: 0 
    Key_name: cim10_indicationGroup_pk 
Seq_in_index: 1 
Column_name: cim10Id 
    Collation: A 
Cardinality: 10137 
    Sub_part: NULL 
     Packed: NULL 
     Null: 
    Index_type: BTREE 
    Comment: 
*************************** 3. row *************************** 
     Table: cim10_indicationgroup 
    Non_unique: 0 
    Key_name: cim10_indicationGroup_pk 
Seq_in_index: 2 
Column_name: indicationGroupId 
    Collation: A 
Cardinality: 10137 
    Sub_part: NULL 
     Packed: NULL 
     Null: 
    Index_type: BTREE 
    Comment: 
*************************** 4. row *************************** 
     Table: cim10_indicationgroup 
    Non_unique: 1 
    Key_name: fk_cim10_indicationGroup_indicationGroup 
Seq_in_index: 1 
Column_name: indicationGroupId 
    Collation: A 
Cardinality: 633 
    Sub_part: NULL 
     Packed: NULL 
     Null: 
    Index_type: BTREE 
    Comment: 

爲什麼有映射例外呢?

回答

1

我認爲你的@ManyToMany映射是錯誤的。 joinColumn指定用於連接源實體('Cim10IndicationGroupCouple')和連接表的列,inverseJoinColumn指定用於連接目標實體('Indicator')的列。試試這個

@ManyToMany 
@JoinTable(
    name = "cim10_indicationgroup_indicator", 
    joinColumns = {@JoinColumn(name = "cim10_indicationgroupId")}, 
    inverseJoinColumns={@JoinColumn(name="indicatorId")} 
) 
private Set<Indicator> indicators; 

我同意這個例外是有誤導性的,特別是如果這個修復了它的話。

+0

謝謝,我會嘗試一下,讓你知道。 – Rolf

+0

仍然越來越:「引起:org.hibernate.MappingException:外鍵(FKCA2E919AF72C81E8:cim10_indicationgroup_indicator [cim10_indicationgroupId]))必須具有與引用的主鍵(cim10_indicationgroup [indicationGroupId,cim10Id])相同的列數」 – Rolf

+0

剛剛碰到在我的項目中同樣的例外。它是由實體中不同字段上的兩個「@ Id」註釋引起的(一個在父類中)。你能證實你的實體中沒有類似的東西嗎? –