JPA派生的复合键

时间:2014-07-14 19:16:35

标签: jpa jdbc orm eclipselink dao

我正在学习JPA 2.1。我使用sql设计了一个数据库,但是使用JPA 2.1无法做到这一点。我列出了sql脚本。

create table SchoolClass (classNo int, constraint schoolClassPK Primary Key (classNo));
create table Section (classNo int, sectionId varchar(4), constraint sectionPK Primary Key(classNo, sectionId), constraint sectionClassNoFK Foreign Key (classNo) References SchoolClass(classNo));
create table Student (classNo int, sectionId varchar(4), rollNo int, name varchar(30), constraint studentPK Primary Key (classNo, sectionId, rollNo), constraint studentClassNoFK Foreign Key (classNo, sectionId) References Section(classNo, sectionId));

我的问题在于实施Student课程。以下是我尝试用JPA做的事情:

@Entity
public class Schoolclass implements Serializable {

@Id
private int classNo;

//other members follow
}



// IdClass
public class SectionCK implements Serializable {

private int classNo;
private String sectionId;

// hashCode(), equals(Object), other members follow
}



@Entity
@IdClass(SectionCK.class)
public class Section implements Serializable {

@Id
private int sectionPK;

@Id
private String sectionId;

@Id
@ManyToOne(cascade = { PERSIST, MERGE })
@JoinColumn(name="SchoolClass_ClassNo", referencedColumnName="classNo")
private SchoolClass classNo;

//other members follow
}



// IdClass
public class StudentCK implements Serializable {

private int classNo;
private String sectionId;
private int rollNo;

// hashCode(), equals(Object), other members follow
}



@Entity
@IdClass(StudentCK.class)
public class Student implements Serializable {

@Id
private int rollNo;

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

@Id
@ManyToOne(cascade = { PERSIST, MERGE })
@JoinColumns({
@JoinColumn(name="Section_SectionId", referencedColumnName="sectionId"),
@JoinColumn(name="Section_ClassNo", referencedColumnName="classNo")
})
private Section sectionId;

//other members follow
}

但是这无法生成上述数据库。需要做哪些改变?

1 个答案:

答案 0 :(得分:0)

无论如何,请尝试从以下代码开始,并朝着所需的映射工作。请注意,@IdClass中字段的名称,类型和数量必须与实体中标记为@Id的字段相匹配。

public class SectionCK {
    private String sectionId;
    private int schoolClass;
}

@Entity
@IdClass(SectionCK.class)
public class Section implements Serializable {
    @Id
    private String sectionId;

    @Id
    @ManyToOne
    @JoinColumn(name="classNo")
    private SchoolClass schoolClass;
}

public class StudentCK implements Serializable {
    private int rollNo;
    private SectionCK section;
}

@Entity
@IdClass(StudentCK.class)
public class Student implements Serializable {
    @Id
    private int rollNo;

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

    @Id
    @ManyToOne
    @JoinColumns({
    @JoinColumn(name="sectionId", referencedColumnName="sectionId"),
    @JoinColumn(name="classNo", referencedColumnName="classNo")
    })
    private Section section;

}

此代码生成以下表格:

Table: Student
COLUMN_NAME         |TYPE_NAME
------------------------------
ROLLNO              |INTEGER  
NAME                |VARCHAR  
CLASSNO             |INTEGER 
SECTIONID           |VARCHAR  

Table: SchoolClass
COLUMN_NAME         |TYPE_NAME
------------------------------
CLASSNO             |INTEGER 

Table: Section
COLUMN_NAME         |TYPE_NAME
------------------------------
SECTIONID           |VARCHAR  
CLASSNO             |INTEGER