Hibernate mappings වලදී table අතර relations, map කිරීම ඉතාම වැදගත් කාර්යයක්.
පහත දැක්වෙන්නේ One-To-One relationship එකක් map කරන ආකාරය
Employee සහ EmployeeDetail නම් class දෙක අතර සම්බන්ධය One-To-One ලෙස ගනිමු.
එනම් එක Employee කෙනෙකුට ඇත්තේ එක EmployeeDetail එකකි.
එලෙසම එක EmployeeDetail එකක් සම්බන්ධ වන්නේ එක Employee කෙනෙක් සමඟ පමණි.
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;
@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;
@OneToOne ( mappedBy="employee" cascade=CascadeType.ALL)
private EmployeeDetail employeeDetail;
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
}
b) package abc.hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name="employeedetail")
public class EmployeeDetail{
@Id
@Column (name="employee_id", unique=true, nullable=false)
@GeneratedValue(generator ="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property",
value="employee"))
private Long employeeId;
@Column(name="street")
private String street;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
@Column(name="country")
private String country;
@OneToOne
@PrimaryKeyJoinColumn
private Employee emploee ;
public EmployeeDetail(){
}
public EmployeeDetail (String street, String city, String state, String country ) {
this.street= street ;
this.city = city ;
this.state = state ;
this.country = country ;
}
//getters and setters
}
No comments:
Post a Comment