Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
annotation @IdClass is used to specify class used as compound key .For example consider the relation between the course and student . students can take many courses and an course can be take by one more than one student .So will need another table we can call it student_course the specify the relation between the student and the course using their primary key.
@ManyToOne @JoinColumn(name ="CRS_ID") private Course course; public Student_Course(){ } public Student_Course(Student student,Course course){ this.stdId=student.getId(); this.student = student; this.crsId=course.getId(); this.course=course; } public long getStdId(){ return stdId; }
public void setStdId(long stdId){ this.stdId = stdId; }
public long getCrsId(){ return crsId; }
public void setCrsId(long crsId){ this.crsId = crsId; }
public Student getStudent(){ return student; }
public void setStudent(Student student){ this.student = student; }
public Course getCourse(){ return course; }
public void setCourse(Course course){ this.course = course; }
}
the compound key table:
Code:
package com.codemiles.jpa;
import java.io.Serializable;
public class StudentCourseId implements Serializable { /** * */ private static final long serialVersionUID = 1L; private long stdId; private long crsId;
public StudentCourseId(){ } public StudentCourseId(long stdId,long crsId){ this.stdId=stdId; this.crsId=crsId; } public long getStdId(){ return stdId; }
public void setStdId(long stdId){ this.stdId = stdId; }
public long getCrsId(){ return crsId; }
public void setCrsId(long crsId){ this.crsId = crsId; } public boolean equals(Object o){ return ((o instanceof StudentCourseId)&& crsId ==((StudentCourseId)o).getCrsId()&& stdId ==((StudentCourseId) o).getStdId()); } public int hashCode(){ return (int)(stdId+crsId); } }
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )