JPA映射注释错误org.hibernate.MappingException:外键必须与引用的主键具有相同的列数

时间:2015-05-26 06:02:57

标签: java hibernate jpa hibernate-mapping

我不能使用JPA注释来映射数据库表。 db

主题地点是ManyToMany至 JoinTable

Subject.java

@Entity
@Table(name = "SUBJECT")
public class Subject implements Serializable {
    @Id
    @Column(name = "SID")
    private Integer sid;

    @Column(name = "NAME")
    private String name;

    // getters and setters
}

SubjectPlace.java

@Entity
@Table(name = "SUBJECT_PLACE")
public class SubjectPlace implements Serializable {
    @Id
    @Column(name = "SPID")
    private Integer spid;

    @ManyToOne
    @JoinColumn(name = "SUB_KEY") //Subject FK
    private Subject subject;

    @ManyToOne
    @JoinColumn(name = "PLC_KEY") //Place FK
    private Place place;

    // getters and setters
}

Place.java

@Entity
@Table(name = "PLACE")
public class Place implements Serializable {
    @Id
    @Column(name = "PID")
    private Integer pid;

    @Column(name = "NAME")
    private String name;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
        @JoinTable(name = "SUBJECT_PLACE",
            joinColumns = { @JoinColumn(name = "PLC_KEY", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "SUB_KEY", nullable = false, updatable = false) })
    private Set<Subject> subjects;

    // getters and setters
}

但我需要在所选位置将主题相关联。我的意思是每个地方都有自己的主题集合。并且一个人有特定位置的主题whitch链接。

像这样:

Subject (M) -- (M) Place通过JoinTable Subject (1) -- (M) Subject_Place (M) -- (1) Place

Person (M) -- (M) Subject_Place通过JoinTable Person (1) -- (M) Person_Subject_Place (M) -- (1) Subject_Place

Person.java

@Entity
@Table(name = "PERSON")
public class Person implements Serializable {
    @Id
    @Column(name = "PRSID")
    private Integer prsid;

    @Column(name = "NAME")
    private String name;

    // How to annotate this code?
    // I experience problem in this part of code
    @OneToMany
    @JoinColumn(name="SPID_KEY") 
    private List<SubjectPlace> subjectPlaces;

    // getters and setters
}

PersonSubjectPlace.java

@Entity
@Table(name = "PERSON_SUBJECT_PLACE")
public class PersonSubjectPlace implements Serializable {
    @Id
    @Column(name = "PSPID") // Person_Subject_Place ID
    private Integer pspid;

    @ManyToOne
    @JoinColumn(name = "PER_KEY") //Person FK
    private Person person;

    // How to annotate this code?
    // I experience problem in this part of code
    @ManyToOne
    @JoinColumn(name = "SPID_KEY") //Subject_Place FK
    private SubjectPlace subjectPlace;

    // getters and setters
}

当我尝试获取人物及其主题时,我收到此错误: Caused by: org.hibernate.MappingException: Foreign key (FK2C3B79384AABC975:PERSON_SUBJECT_PLACE [SPID_KEY])) must have same number of columns as the referenced primary key (SUBJECT_PLACE [PLC_KEY,SUB_KEY])

什么,我该如何绘制?

2 个答案:

答案 0 :(得分:0)

您应删除$(window).scroll(function() { $(".bounceInRight h2").each(function(){ var eyemoveright = $(this).offset().top; if($(window).scrollTop() > eyemoveright && !$(this).hasClass('reached')) { console.log('right'); $(this).addClass('reached'); $('.blink').addClass( "move-right"); $('.blink1').addClass( "move-right"); $('.blink').removeClass( "move-left"); $('.blink1').removeClass( "move-left"); } }) $(".bounceInLeft h2").each(function(){ var eyemoveleft = $(this).offset().top; if($(window).scrollTop() > eyemoveleft && !$(this).hasClass('reached')) { console.log('left'); $(this).addClass('reached'); $('.blink').addClass( "move-left"); $('.blink1').addClass( "move-left"); $('.blink').removeClass( "move-right"); $('.blink1').removeClass( "move-right"); } }) }); 注释,并将@Joincolumn变量添加到mappedBy注释。

@OneToMany

您应该在@OneToMany(mappedBy = "spid") 中有一个变量SubjectPlace,您应该在其中添加Person注释

答案 1 :(得分:0)

在您的OneToMany映射中,您不需要指定外键,只需使用mappedBy属性来引用您的映射对象,您可以在{{了解更多相关信息3}},以及这里映射PersonPersonSubjectPlace实体所需的内容:

在您的Person类中:

@OneToMany(mappedBy="person")
private List<PersonSubjectPlace> personsubjectPlaces;

在您的PersonSubjectPlace类中:

@ManyToOne
@JoinColumn(name="PRSID") //Specify the primary key of Person
private Person person;

有关JoinColumnmappedBy之间差异的详细信息,请查看OneToMany Mapping Documentation

编辑:

对于SubjectPlacePersonSubjectPlace之间的映射:

在您的SubjectPlace类中:

@OneToMany(mappedBy="subjectPlace")
private List<PersonSubjectPlace> personsubjectPlaces;

在您的PersonSubjectPlace类中:

@ManyToOne
@JoinColumn(name="SPID") //Specify the primary key of SubjectPerson
private SubjectPlace subjectPlace;

注意:

映射这些类的最佳方法是在@JoinTablePerson之间使用SubjectPlace,看看this answer,因为PersonSubjectPlace是实际的PersonSubjectPlace之间的联合实体。