MappingException休眠映射列

时间:2018-08-10 09:12:43

标签: java mysql hibernate mariadb

我正在努力解决以下问题:

  

由于:org.hibernate.MappingException:外键   (FKj4uw5b6ekvxc2djohvon7lk7:bi_person_country_countries   [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;

1 个答案:

答案 0 :(得分:0)

PersonCountry@ManyToMany两种情况下都使用Person时,您不需要这里的Country类。

如果由于某种原因必须保留它。.链接表中不应包含@OneToMany / @ManyToMany映射,因此您将:

@ManyToOne
private Person person;

@ManyToOne
private Country country;

请记住,如果数据库名称不同于person_idcountry_id,则可能还需要使用@JoinColumn。