2016-11-26 101 views
1

我在我的數據庫中有3個表格:教授,學科和課程。 從教授和學科到課程,這是多對一的關係。 我試圖把外鍵,但它不起作用。在jpa中添加外鍵和複合主鍵?

課程類:

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

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類:

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) 
    ) 

因此,外鍵不會出現。請幫我

+0

哪裏是另一個表?我認爲你需要申報課程和課程教授課程。 – msagala25

+0

@msagala編輯,尋找紀律類 –

回答

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;