Total members 11893 |It is currently Fri Nov 15, 2024 1:34 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.

Here are the entities classes :

Student class :
Code:

package com
.codemiles.jpa;

import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.OneToMany;

@
Entity
@Table(name="STUDENT")
public class Student implements java.io.Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private long Id
;
    private String name;
    private Date birthday;
    private String gender;
    
    
    
@OneToMany(mappedBy="student")
    private Collection<Student_Course> student_courses;
    public long getId() {
        return Id;
    }
    public void setId(long id) {
        Id = id;
    }
    @Column(name="NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @
Temporal(TemporalType.DATE)
    @Column(name="BIRTHDAT")
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Column(name="GENDER")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void setStudent_courses(Collection<Student_Course> student_courses) {
    this.student_courses = student_courses;
    }
    public Collection<Student_Course> getStudent_courses() {
    return student_courses;
    }
}

 



Course
Code:

package com
.codemiles.jpa;

import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.OneToMany;
@
Entity
@Table(name="COURSE")
public class Course implements java.io.Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private long Id
;
    private String name;
    private Long credit;
    private Date startDate;
    private String department;
    @OneToMany(mappedBy="course")
    private Collection<Student_Course> student_courses;
 
    public  long getId
() {
        return Id;
    }
    public void setId( long id) {
        Id = id;
    }
    @Column(name="NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Column(name="CREDIT")
    public Long getCredit() {
        return credit;
    }
    public void setCredit(Long credit) {
        this.credit = credit;
    }

    @
Temporal(TemporalType.DATE)
    @Column(name="START_DATE")
    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    @Column(name="DEPARTMENT")
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public void setStudent_courses(Collection<Student_Course> student_courses) {
    this.student_courses = student_courses;
    }
    public Collection<Student_Course> getStudent_courses() {
    return student_courses;
    }
   
}

 


the relation table .

Code:

package com
.codemiles.jpa;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@
Entity
@IdClass(StudentCourseId.class)
@
Table(name="STUDENT_COURSE")
public class Student_Course {

    @Id
    
@Column(name = "STD_ID", insertable = false, updatable = false)
    private long stdId;

    @Id
    
@Column(name = "CRS_ID", insertable = false, updatable = false)
    private long crsId;
    @ManyToOne
    
@JoinColumn(name = "STD_ID")
     private Student student;

    @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);
    }
}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Compound Key and IdClass annotation
 @Temporal JPA annotation     -  
 @Transient annotation in JPA entity     -  
 Id reference annotation relationship     -  
 @QueryHint annotation to customize the query     -  



Topic Tags

Java JPA
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com