Wednesday, July 25, 2012

4. Hibernate mappings, Annotations භාවිතයෙන්...



පහත දැක්වෙන Employee class එක simple POJO class එකක්.

මෙහිදී hibernate mapping සිදු කර තිබෙන්නේ annotations භාවිතයෙන්.



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
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;

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

}

ඉහත  annotations වල භාවිතය පහත පරිදිය.

@Entity -  මෙමඟින් class එක entity එකක් බව ( persistent POJO class එකක් බව ) ප්‍රකාශ වෙයි.

@Table(name="xxx") -  class එක  map වියයුතු table එකේ නම


@Id -  මෙමඟින් මේ entity එකේ identifier property එක ප්‍රකාශ කරයි.

@GeneratedValue - primary key එක auto generate විය යුතු බව

@Column(name="xxx") -  attribute එක  map වියයුතු column එකේ නම




one-to-one, one-to-many සහ many-to-many relationships ඇති tables, map කරන ආකාරය ඉදිරියට...









No comments:

Post a Comment