我正在努力解决以下问题:
(由:(FKj4uw5b6ekvxc2djohvon7lk7:bi_person_country_countries ( org.hibernate.MappingException: Foreign person_country_id)引起))必须有与引用的主键相同的列数(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;PersonCountry:
@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;发布于 2018-08-10 09:31:21
这里不需要PersonCountry类,因为在Person和Country映射这两种情况下都使用@ManyToMany。
如果你因为某种原因不得不保留它..。链接表不应该包含@OneToMany / @ManyToMany映射,因此您应该有:
@ManyToOne
private Person person;
@ManyToOne
private Country country;请记住,如果数据库名与person_id和country_id不同,也可能需要使用@JoinColumn。
https://stackoverflow.com/questions/51782969
复制相似问题