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.

package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
public class Person implements Serializable {
	private static final long serialVersionUID = 1L;
	private String firstName;
	private String lastName;
	public Person(String firstName,String 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;

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;

public class Student implements Serializable {
	private static final long serialVersionUID = 1L;
	@AttributeOverride(name="firstName", column=@Column(name="f_name"))
	private Person id;
	private String location;
	public Student(Person id, String 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;

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()
		} 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();
		Person p= new Person("naresh", "rai");
		Student s= new Student(p, "varanasi");
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