java - MappingException hibernate 映射列

标签 java mysql hibernate mariadb

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

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 类,因为在 PersonCountry 两种情况下都使用 @ManyToMany > 映射。

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

@ManyToOne
private Person person;

@ManyToOne
private Country country;

请记住,如果数据库名称与 person_idcountry_id 不同,您也可能需要使用 @JoinColumn。

关于java - MappingException hibernate 映射列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782969/

相关文章:

java - Android 和 AppEngine 网络服务 : Json. .. RPC、REST... Protocol Buffer ?

mysql - PHP/MySQL – 连接到服务器时出错

hibernate 5 序列生成器没有给出正确的值

Spring data jpa Query动态传递where子句

java - Hibernate 4.0 的事务锁定问题

java - RxJava2多线程或者出现问题

带有嵌入式 Jetty 和 Jersey(无 GAE)的 java.lang.IncompatibleClassChangeError

java - 使用WebService客户端出现异常。(MyEclipse)

php - RecyclerView 无限滚动与 Volley

mysql同一张表上的小计数问题