Thursday, July 26, 2012

6.One-To-Many relation mapping


පහත දැක්වෙන්නේ One-To-Many relationship එකක් map කරන ආකාරය.


Department සහ Employee classes දෙක අතර සම්බන්ධය One-To-Many ලෙස ගනිමු.

එනම් එක Department එකක Employees ලා බොහෝ සිටිය හැක.
නමුත් එක Employee කෙනෙක් අයත් වන්නේ එක Department එකකට පමණි.

a)  package abc.hibernate;

import java.sql.Date;

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

@Entity
@Table(name="employee")
public class Employee{

@Id
@GeneratedValue
@Column(name="emp_id")
private Long empId;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="birth_date")
private Date birth_date;

@Column(name="salary")
private double salary;

@ManyToOne
@JoinColumn(name="deptment_id")
private Department Department;


public Employee(){
}

public Employee(String firstname, String lastname, Date birthdate, double salary){
this.firstName = firstname;
this.lastName = lastname ;
this.birthDate = birthdate ;
this.salary = salary ;
}

//Getter and Setter methods

}






@JoinColumn යනු Entity දෙකක් සම්බන්ධ කිරීමේදී map වෙන column එකයි.
----------------------------------------------------------------------------




b)   package abc.hibernate;

import java.util.Set;

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


@Entity
@Table(name="department")
public class Department{


@Id
@GeneratedValue
@Column(name="department_id")
private Long departmentId;




@Column(name="depat_name")
private String departmentName;




@OneToMany(mappedBy="department")
private Set<Employee> employee ;


//getters and setters


}



























































No comments:

Post a Comment