外键(FK300D262149997B:region_location [region])的列数必须与引用的主键(region_location [region,location])
1)。创建具有两列(主键和名称字段)的表“ region”。 2)。创建具有两列(主键和名称字段)的表“位置”。 3)。创建具有三列的表“ region_location”(主键,表“ region”的外键引用和表“ location”的外键引用)。 4)。创建具有三列(主键,名称字段,表'region_location'的外键引用和名称字段)的表'Area'。
/**
* Region model
*/
@Entity
@Data
@EqualsAndHashCode(of = "id")
@Table(name = "region")
public class Region {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name")
@NotNull
@Size(min = 2, max = 255)
private String name;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
@JoinTable(name = "region_location", joinColumns = @JoinColumn(name = "region", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "location", referencedColumnName = "id"))
private Set<Location> location;
}
/**
* Location model
*/
@Entity
@Data
@ToString
@EqualsAndHashCode(of = "id")
@Table(name = "location")
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name")
@NotNull
@Size(min = 2, max = 255)
private String name;
@Column(name = "pin_code")
@NotNull
@Min(value = 100000)
@Max(value = 999999)
private Integer pinCode;
@ManyToMany(mappedBy = "location", fetch = FetchType.EAGER)
private Set<Region> region;
}
/**
* Region Location model
*/
@Entity
@Data
@ToString
@EqualsAndHashCode(of = "id")
@Table(name = "region_location")
public class RegionLocation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "location")
private Location location;
@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "region")
private Region region;
}
/**
* Area model
*/
@Entity
@Data
@ToString
@EqualsAndHashCode(of = "id")
@Table(name = "area")
public class Area {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
/**
** problem code
**/
@ManyToOne(optional = true, fetch = FetchType.EAGER)
@JoinColumn(name = "region_location")
private RegionLocation regionLocation;
}
当我尝试使用主类进行测试时,遇到诸如休眠映射错误之类的映射错误,例如外键(FK5110401A8398947:area [region_location_id]))必须具有与引用的主键相同的列数(region_location [region_id] ,location_id])。
当我在区域中映射region_location表时,我知道区域表中会出现错误。如何使它起作用?
预先感谢