@Parent in Hibernate Annotation
February 05, 2013
In Hibernate Annotation, while using embeddable object we can assign parent class reference in embeddable class. @Parent annotation is used to assign the parent reference. We can call this reference through the parent class. In the below example we have an Entity that will embed the Address entity.
package com.concretepage.persistence; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.Parent; @Embeddable @Table(name="address") public class Address implements Serializable { private static final long serialVersionUID = 1L; @Id private int addId; @Column(name = "city") private String city; @Column(name = "country") private String country; @Parent public Person person; }
package com.concretepage.persistence; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="person") public class Person implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="person_id") private int personId; @Column(name="name") private String name; @Embedded public Address address; }