One to many relationships in EJB
I have two EBs related by a one to many relationship. The first of these tables is called Student and has studentId as its key. The second of the tables has a composite key made up of studentID, courseID and semesterID.
However, when the tables are generated in NetBeans 5.5, I find that the Course table has studentID as both a primary key and a foreign key.
I am at a loss to understand this as I am new to NetBeans and EJB.
Can anyone advise?
Thanks
Martin O'Shea.
The Student class:
package CBSD_CW;
import java.io.Serializable;
import javax.persistence.*;
import java.util.*;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
publicclass Student_EBimplements Serializable{
// Table columns.
@Id
@Column(name ="STUDENT_ID", nullable =false)
private Long studentId;// Student ID. Entity <Student><ID> from file: SampleData.xml.
@Column(name ="FIRST_NAME", length = 50, nullable =true)
private String firstName;// First Name. Entity <Student><FirstName> from file: SampleData.xml.
@Column(name ="LAST_NAME", length = 50, nullable =true)
private String lastName;// Last Name. Entity <Student><LastName> from file: SampleData.xml.
@Column(name ="STUDENT_LEVEL", length = 2, nullable =true)
private String studentLevel;// Student Level. Entity <Student><Level> from file: SampleData.xml.
@Column(name ="PROGRAM_NAME", length = 50, nullable =true)
private String programName;// Program Name. Entity <Student><ProgramName> from file: SampleData.xml.
@Column(name ="PROGRAM_NUMBER", length = 3, nullable =true)
private String programNumber;// Program Number. Entity <Student><ProgramNumber> from file: SampleData.xml.
// 1:M relationship with Course_EB.
@OneToMany(cascade=CascadeType.ALL, mappedBy="student")
private List<Course_EB> courses;
public List<Course_EB> getCourses(){
return courses;
}
publicvoid setCourses(List<Course_EB> courses){
this.courses = courses;
}
// Accessors and mutators.
public Long getId(){
return this.studentId;
}
publicvoid setId(Long studentId){
this.studentId = studentId;
}
public String getFirstName(){
return firstName;
}
publicvoid setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
publicvoid setLastName(String lastName){
this.lastName = lastName;
}
public String getStudentLevel(){
return studentLevel;
}
publicvoid setStudentLevel(String studentLevel){
this.studentLevel = studentLevel;
}
public String getProgramName(){
return programName;
}
publicvoid setProgramName(String programName){
this.programName = programName;
}
public String getProgramNumber(){
return programNumber;
}
publicvoid setProgramNumber(String programNumber){
this.programNumber = programNumber;
}
// Other methods.
publicboolean equals(Object obj){
if (obj ==this){
return (true);
}
if (!(objinstanceof Student_EB)){
return (false);
}
if (obj ==null){
return (false);
}
Student_EB student = (Student_EB) obj;
return (student.studentId == studentId);
}
The Courseclass:
package CBSD_CW;
import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
@Entity
publicclass Course_EBimplements java.io.Serializable{
Course_PK course;
// M:1 relationship with Student_EB.
private Student_EB student;
@ManyToOne(cascade=CascadeType.ALL)
@JoinTable(name ="Student_EB", joinColumns= @JoinColumn(name ="STUDENT_ID", referencedColumnName ="studentId"))
public Student_EB getStudent(){
return (student);
}
publicvoid setStudent(Student_EB student){
this.student = student;
}
// Constructor.
public Course_EB(){
}
// Accessors and mutators.
@EmbeddedId
public Course_PK getCourse_PK (){
return (course);
}
publicvoid setCourse_PK(Course_PK course){
this.course = course;
}
}
The Course_PKclass:
package CBSD_CW;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
publicclass Course_PKimplements java.io.Serializable{
// Table columns.
@Column(name ="STUDENT_ID", length = 4, nullable =false)
privatelong studentId;// Student ID. Entity <Student><ID> from file: SampleData.xml.
@Column(name ="COURSE_ID", length = 4, nullable =false)
private String courseId;// Course ID. Entity <Student><Course><Number> from file: SampleData.xml.
@Column(name ="SEMESTER_CODE", length = 3, nullable =false)
private String semesterCode;// Semester Code. Entity <Student><Course><SemesterCode> from file: SampleData.xml.
@Column(name ="GRADE", nullable =true)
privatechar grade;// Grade. Entity <Student><Course><Grade> from file: SampleData.xml.
// Constructors.
public Course_PK(){
}
public Course_PK(long studentId,long courseid, String semesterCode){
this.studentId = studentId;
this.courseId = courseId;
this.semesterCode = semesterCode;
}
// Accessors and mutators.
publiclong getStudentId(){
return (studentId);
}
publicvoid setStudentId(long studentId){
this.studentId = studentId;
}
public String getCourseId(){
return (courseId);
}
publicvoid setId(String courseId){
this.courseId = courseId;
}
public String getSemesterCode(){
return (semesterCode);
}
publicvoid setSemesterCode(String semesterCode){
this.semesterCode = semesterCode;
}
publicchar getGrade(){
return (grade);
}
publicvoid setGrade(char grade){
this.grade = grade;
}
// Other methods.
publicboolean equals(Object obj){
if (obj ==this){
return (true);
}
if (!(objinstanceof Course_PK)){
return (false);
}
if (obj ==null){
return (false);
}
Course_PK course = (Course_PK) obj;
return ((course.studentId == studentId) &&
(course.courseId.equals(courseId)) &&
(course.semesterCode.equals(semesterCode)) &&
(course.grade == grade));
}
}

