在jpa中添加外键和复合主键?

时间:2016-11-26 16:22:37

标签: java jpa foreign-keys composite-primary-key

我的数据库中有3个表:教授,学科和课程。 从教授和学科到课程,它是多对一的关系。 我试图把外键,但它不起作用。

课程类:

package com.licenta.ascourses.model;

import java.io.Serializable;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="Courses")
@AssociationOverrides({
    @AssociationOverride(name = "pk.discipline",
        joinColumns = @JoinColumn(name = "IDDISCIPLINE")),
    @AssociationOverride(name = "pk.professor",
        joinColumns = @JoinColumn(name = "IDPROFESSOR")) })
public class Course implements Serializable {



    private CourseId idCourse=new CourseId();
    private int year;
    private int semester;


    public Course()
    {


    }


    public Course(CourseId idCourse, int idDiscipline, int idProfessor,int year, int semester) {
        super();
        this.idCourse = idCourse;
        this.year = year;
        this.semester = semester;
    }


    @EmbeddedId
    public CourseId getIdCourse() {
        return idCourse;
    }


    public void setIdCourse(CourseId idCourse) {
        this.idCourse = idCourse;
    }


    public int getYear() {
        return year;
    }


    public void setYear(int year) {
        this.year = year;
    }


    public int getSemester() {
        return semester;
    }


    public void setSemester(int semester) {
        this.semester = semester;
    }


}

复合主键的CourseId类:

package com.licenta.ascourses.model;

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


@Embeddable
public class CourseId implements Serializable{

        @ManyToOne
        @JoinColumn(name="IDDISCIPLINE")
        private int idDiscipline;

        @ManyToOne
        @JoinColumn(name="IDPROFESSOR")
        private int idProfessor;
        private String courseNo;



        public int getIdDiscipline() {
            return idDiscipline;
        }

        public void setIdDiscipline(int idDiscipline) {
            this.idDiscipline = idDiscipline;
        }

        public int getIdProfessor() {
            return idProfessor;
        }

        public void setIdProfessor(int idProfessor) {
            this.idProfessor = idProfessor;
        }

        public String getCourseNo() {
            return courseNo;
        }

        public void setCourseNo(String courseNo) {
            this.courseNo = courseNo;
        }

        public boolean equals(Object o) {

            return true;
        }

        public int hashCode() {

            return 1;
        }


}

纪律课程:

package com.licenta.ascourses.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;



@Entity
@Table(name="Disciplines")
public class Discipline implements Serializable {
    private int idDiscipline;
    @Id
    private String name;
    private String description;
    private int an;
    private int semestru;




    public Discipline()
    {

    }

    public Discipline(int idDiscipline, String name, String description, int an, int semestru) {
        super();
        this.idDiscipline = idDiscipline;
        this.name = name;
        this.description = description;
        this.an = an;
        this.semestru = semestru;
    }




    public Discipline(int idDiscipline, String name, String description, int an, int semestru, Set<Course> courses) {
        super();
        this.idDiscipline = idDiscipline;
        this.name = name;
        this.description = description;
        this.an = an;
        this.semestru = semestru;

    }


    public int getIdDiscipline() {
        return idDiscipline;
    }


    public void setIdDiscipline(int idDiscipline) {
        this.idDiscipline = idDiscipline;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getDescription() {
        return description;
    }


    public void setDescription(String description) {
        this.description = description;
    }


    public int getAn() {
        return an;
    }


    public void setAn(int an) {
        this.an = an;
    }


    public int getSemestru() {
        return semestru;
    }


    public void setSemestru(int semestru) {
        this.semestru = semestru;
    }



}

这就是我的课程表是由Hibernate制作的:

create table Courses (
        courseNo varchar2(255 char) not null,
        idDiscipline number(10,0) not null,
        idProfessor number(10,0) not null,
        semester number(10,0) not null,
        year number(10,0) not null,
        primary key (courseNo, idDiscipline, idProfessor)
    )

因此,外键不会出现。请帮帮我

1 个答案:

答案 0 :(得分:0)

尝试改变这一点:

@ManyToOne
@JoinColumn(name="IDDISCIPLINE")
private int idDiscipline;

@ManyToOne
@JoinColumn(name="IDPROFESSOR")
private int idProfessor;
private String courseNo;

到此:

@ManyToOne
@JoinColumn(name="idDiscipline")
private Discipline discipline;

@ManyToOne
@JoinColumn(name="idProfessor")
private Professor professor;
private String courseNo;