我正在努力解决以下问题:
Caused by: org.hibernate.MappingException: Foreign key (FKj4uw5b6ekvxc2djohvon7lk7:bi_person_country_countries [person_country_id])) must have same number of columns as the referenced primary key (bi_person_country [country_id,person_id])
我创建了 4 个模型:
@Table(name = "bi_country")
@Entity
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "bi_person_country", joinColumns = @JoinColumn(name = "country_id"), inverseJoinColumns = @JoinColumn(name = "person_id"))
private Set<Person> persons;
性别:
@Table(name = "bi_gender")
@Entity
public class Gender {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
public Integer getId() {
return id;
}
人:
@Table(name = "bi_person")
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name = "additional_info")
private String additionalInfo;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "persons")
private Set<Country> countries;
@ManyToOne
private Gender gender;
人物国家/地区:
@Table(name = "bi_person_country")
@Entity
public class PersonCountry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
private Person person;
@ManyToMany
private List<Country> countries;
最佳答案
此处不需要 PersonCountry
类,因为在 Person
和 Country
两种情况下都使用 @ManyToMany
> 映射。
如果由于某种原因您必须保留它..链接表不应包含 @OneToMany
/@ManyToMany
映射,因此您将拥有:
@ManyToOne
private Person person;
@ManyToOne
private Country country;
请记住,如果数据库名称与 person_id
和 country_id
不同,您也可能需要使用 @JoinColumn。
关于java - MappingException hibernate 映射列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782969/