Pages

Friday, October 30, 2009

Hibernate Annotation - @Embedded and @Embeddable

Share it Please
In this blog, I will explain you how to map a component using @Embedded and @Embeddable Annotations.
Consider the Student Object which contains columns ,
StudentID
StudentName
StudentClass
StudentStreet
StudentCity
StudentState

We can see the columns StudentStreet , city and state can be kept in one more object “Address”.where as the Student Object contains only StudentID,StudentName and StudentClass.
So now we can make these fields into an Address object , but still we need to save these values in to a student object for which the details belong.for this we need to make sure that the address object created for a student should be inserted into the student as a internal component.
The @Embedded annotation says the same thing , when ever we define an child object as a @Embedded inside a parent object, the child object is said to saved as a component

@Embedded annotation says that the object should be saved as internal component to other object .
@Embeddable annotation says that the object defined as @Embeddable will be saved as an internal component of another object.the object defined as a @Embeddable will not have any Primary key since it will be saved according to the parent object.

Now consider the Student Object

Public class Student implements Serializable {

Private int StudentID;

public Student() {

}

public Student(String name,String studentClass ,Address address) {
this.studentName=name;
this.studentClass=studentClass;
this.address=address;
}

@Id
@GeneratedValue
private int ID;

private String studentName;
private String studentClass;
private String studentAge;

public int getID() {
return ID;
}

public void setID(int id) {
ID = id;
}

public String getStudentAge() {
return studentAge;
}

public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}

public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentClass() {
return studentClass;
}
public void setStudentClass(String studentClass) {
this.studentClass = studentClass;
}


@Embedded
private Address address;
public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}


}


we can see from the above the address object is defined with an @Embedded annotation to make that address object saved as a internal component of student object.
Now consider the Address class ,
@Embeddable
public class Address implements Serializable {


private String Street;
private String City;
private String State;

public String getStreet() {
return Street;
}

public void setStreet(String street) {
Street = street;
}

public String getCity() {
return City;
}

public void setCity(String city) {
City = city;
}

public String getState() {
return State;
}

public void setState(String state) {
State = state;
}

public Address() {
}

public Address(String street,String city,String state) {
this.Street=street;
this.City=city;
this.State=state;
}

}

@Embeddable annotation says that the object will be treated as a component.

All these columns will be saved in a single table in database. No 2 tables.

1 comment :