I'm struggling with following issue:
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])
I created 4 models:
@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;
Gender:
@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;
}
Person:
@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;
You dont need the PersonCountry
class here as you are using @ManyToMany
in both cases being Person
and Country
mappings.
If you have to keep it for some reason.. the linking table should not contain @OneToMany
/ @ManyToMany
mappings, so you would have:
@ManyToOne
private Person person;
@ManyToOne
private Country country;
Keep in mind you may need to use @JoinColumn also if the database names are different than person_id
and country_id
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.