Home > Hibernate

Example of @EmbeddedId in Hibernate

May 19, 2013
@EmbeddedId is used for composite primary key. i.e. more than one column behaves jointly as primary key. We need to create an entity as Embeddable and the Embeddable entity can be embedded in other entity to achieve composite primary key.

Person.java
package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class Person implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@Column(name="first_name")
	private String firstName;
	
	@Column(name="last_name")
	private String lastName;
	
	public Person(String firstName,String lastName){
		this.firstName=firstName;
		this.lastName=lastName;
	}
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}
 


Student.java
package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student implements Serializable {
	private static final long serialVersionUID = 1L;
	@EmbeddedId
	@AttributeOverride(name="firstName", column=@Column(name="f_name"))
	private Person id;
	
	@Column(name="location")
	private String location;
	
	public Student(Person id, String location){
		this.id=id;
		this.location=location;
	}
	public Person getId() {
		return id;
	}
	public void setId(Person id) {
		this.id = id;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
}
 


HibernateUtil.java
package com.concretepage.util;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.concretepage.persistence.Person;
import com.concretepage.persistence.Student;

public class HibernateUtil {
	private static final SessionFactory concreteSessionFactory;
	static {
		try {

			Properties prop= new Properties();
			prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
			prop.setProperty("hibernate.connection.username", "root");
			prop.setProperty("hibernate.connection.password", "");
			prop.setProperty("hibernate.hbm2ddl.auto", "update");
			prop.setProperty("show_sql", "true");
			prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
			
			concreteSessionFactory = new AnnotationConfiguration()
		   .addPackage("com.concretepage.persistence")
				   .addProperties(prop)
				   .addAnnotatedClass(Person.class)
				   .addAnnotatedClass(Student.class)
				   .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();
		Person p= new Person("naresh", "rai");
		Student s= new Student(p, "varanasi");
		session.save(s);
		session.getTransaction().commit();
		session.close();
	}
}
 
comments powered by Disqus



Home    Core Java    Spring    Hibernate    Jquery    My Computer    News    Forum    Quiz    Interview Q & A     Maven     Eclipse IDE

Copyright ©2014 concretepage.com | All rights reserved |Privacy

Developed by : TechWox Corporation