我一直面临着一个我不理解的映射异常。 这是一个例外:
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:
为什么会出现映射异常?
答案 0 :(得分: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;
我同意该异常具有误导性,特别是如果这样做会解决它。