Home  >  Hibernate

Example of @Embeddable and @Embedded in Hibernate Annotation

By Arvind Rai, May 12, 2013
One entity can be embedded in another entity. The attributes of an entity can be common attributes of more than one entity. In this case there can be one embeddable entity. And this embeddable entity can be embedded in more than one entity.
Let's understand by example. We have one Animal entity, it has name and location attributes. Now two different entity Lion and Elephant can have Animal attributes just by embedding the Animal Entity. We can override the attributes. In animal entity there is location attribute and in elephant there is place attribute. So With the help of @AttributeOverrides we can do like below.
 @AttributeOverrides({ @AttributeOverride(name = "location", column = @Column(name = "place")) })
 

Animal.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class Animal {
	@Column(name = "name")
	private String name;
	@Column(name = "location")
	private String location;
	public Animal(){
	}
	public Animal(String name,String location){
		this.name=name;
		this.location=location;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
}
 

Elephant.java
package com.concretepage.persistence;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "elephant")
public class Elephant {
	@Id
	@Column(name = "id")
	private int id;
	@Embedded
	@AttributeOverrides({ @AttributeOverride(name = "location", column = @Column(name = "place")) })
	private Animal animal;
	public Elephant(int id,Animal animal){
		this.id=id;
		this.animal=animal;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Animal getAnimal() {
		return animal;
	}
	public void setAnimal(Animal animal) {
		this.animal = animal;
	}
} 

Lion.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="lion")
public class Lion {
	@Id
	@Column(name = "id")
	private int id;
	@Embedded
	private Animal animal;
	public Lion(int id,Animal animal){
		this.id=id;
		this.animal=animal;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Animal getAnimal() {
		return animal;
	}
	public void setAnimal(Animal animal) {
		this.animal = animal;
	}
} 

hibernate.cfg.xml
 <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.url">
    jdbc:mysql://localhost:3306/hibernate</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="com.concretepage.persistence.Animal"/>
    <mapping class="com.concretepage.persistence.Lion"/>
    <mapping class="com.concretepage.persistence.Elephant"/>
   </session-factory>
</hibernate-configuration>
 

HibernateUtil.java
package com.concretepage.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.concretepage.persistence.Animal;
import com.concretepage.persistence.Elephant;
import com.concretepage.persistence.Lion;
public class HibernateUtil {
	private static final SessionFactory concreteSessionFactory;
	static {
		try {
			concreteSessionFactory = new AnnotationConfiguration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}
	public static Session getSession() throws HibernateException {
		return concreteSessionFactory.openSession();
	}
	public static void main(String... args) {
		Session session = getSession();
		session.beginTransaction();
		Animal animal = new Animal("Lion A", "Africa");
		Lion lion = new Lion(1, animal);
		animal = new Animal("Elephnat A", "Asia");
		Elephant elephant = new Elephant(1, animal);
		session.save(lion);
		session.save(elephant);
		session.getTransaction().commit();
		session.close();
	}
} 
Output

Example of @Embeddable and @Embedded in Hibernate Annotation
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS








Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us