【问题标题】:MappingException hibernate mapping columnsMappingException 休眠映射列
【发布时间】:2019-01-17 20:39:10
【问题描述】:

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

原因: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;

个人国家:

@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;

【问题讨论】:

    标签: java mysql hibernate mariadb


    【解决方案1】:

    您不需要PersonCountry 类,因为您在这两种情况下都使用@ManyToManyPersonCountry 映射。

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

    @ManyToOne
    private Person person;
    
    @ManyToOne
    private Country country;
    

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

    【讨论】:

    • 感谢 Maćku 的好回答。我删除了我的 PersonCountry 类,现在我得到了:无法提取 ResultSet; SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet" 。我的类应该是什么样子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    相关资源
    最近更新 更多