mappingexception hibernateMap列

jxct1oxe  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(212)

我正在努力解决以下问题:
原因:org.hibernate.mappingexception:外键(fkj4uw5b6ekvxc2djohvon7lk7:biu person\u country\u countries[person\u country\u id])必须与引用的主键(biu person\u country[country\u id,person\u 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;
b0zn9rqh

b0zn9rqh1#

你不需要 PersonCountry 在这里使用 @ManyToMany 在这两种情况下 Person 以及 Country Map。
如果你因为某种原因不得不保留它。。链接表不应包含 @OneToMany / @ManyToMany Map,所以您可以:

@ManyToOne
private Person person;

@ManyToOne
private Country country;

请记住,如果数据库名称不同于 person_id 以及 country_id .

相关问题