Adding foreign key and composite primary key in jpa?
<p>I have 3 tables in my database: Professors, Disciplines and Courses.
From Professors and Disciplines to Courses it is many to one relationship.
I have tried to put foreign keys, but it does not work.</p>

<p>Course class:</p>

<pre><code>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;
    }


}
</code></pre>

<p>CourseId class for composite primary key:</p>

<pre><code>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;
        }


}
</code></pre>

<p>Discipline class:</p>

<pre><code>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&lt;Course&gt; 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;
    }



}
</code></pre>

<p>And this is how my Course table is made by Hibernate:</p>

<pre><code>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)
    )
</code></pre>

<p>So, the foreign keys does not appear. Please help me</p>