首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MappingException hibernate映射列

MappingException hibernate映射列
EN

Stack Overflow用户
提问于 2018-08-10 09:12:42
回答 1查看 47关注 0票数 0

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

(由:(FKj4uw5b6ekvxc2djohvon7lk7:bi_person_country_countries ( org.hibernate.MappingException: Foreign person_country_id)引起))必须有与引用的主键相同的列数(bi_person_country country_id,person_id)

我创建了4个模型:

代码语言:javascript
复制
@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;

性别:

代码语言:javascript
复制
@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;
    }

人:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@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;
EN

回答 1

Stack Overflow用户

发布于 2018-08-10 09:31:21

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

如果你因为某种原因不得不保留它..。链接表不应该包含@OneToMany / @ManyToMany映射,因此您应该有:

代码语言:javascript
复制
@ManyToOne
private Person person;

@ManyToOne
private Country country;

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51782969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档