@Embedded and @Embeddable Example in Hibernate

The Embedded and Embeddable Example in Hibernate

In this tutorial, I am attaching simple application about, usage of Embedded and Embeddable Example in Hibernate framework. The application were tested with MySQL database environment and shared in the same post.

Project Structure

Embedded and Embeddable Example in Hibernate



package com.dineshkrish.hibernate;

import javax.persistence.Embeddable;

public class Address {

	private String city;
	private String state;
	private String country;
	private String zipCode;

	public String getCity() {
		return city;

	public void setCity(String city) {
		this.city = city;

	public String getState() {
		return state;

	public void setState(String state) {
		this.state = state;

	public String getCountry() {
		return country;

	public void setCountry(String country) {
		this.country = country;

	public String getZipCode() {
		return zipCode;

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;




package com.dineshkrish.hibernate;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

public class Customer {

	private int customerId;

	private String customerName;

	private int customerAge;

	private Address customerAddress;

	public Address getCustomerAddress() {
		return customerAddress;

	public void setCustomerAddress(Address customerAddress) {
		this.customerAddress = customerAddress;

	public int getCustomerId() {
		return customerId;

	public void setCustomerId(int customerId) {
		this.customerId = customerId;

	public String getCustomerName() {
		return customerName;

	public void setCustomerName(String customerName) {
		this.customerName = customerName;

	public int getCustomerAge() {
		return customerAge;

	public void setCustomerAge(int customerAge) {
		this.customerAge = customerAge;


Getting Session for Transaction


package com.dineshkrish.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

 * @author Dinesh Krishnan

public class SessionProvider {

	private static Session session;
	private static Configuration configuration;
	private SessionProvider() { }
	static {
		configuration = new Configuration()
		.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
	    .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
	    .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/dineshkrish")
	    .setProperty("hibernate.connection.username", "root")
	    .setProperty("hibernate.connection.password", "")
	    .setProperty("hibernate.show_sql", "true")
	    .setProperty("hibernate.hbm2ddl.auto", "update")
	    // Your Mapping Class
	public static Session getSession() {
		SessionFactory factory = configuration.buildSessionFactory();
		session = factory.openSession();
		return session;


package com.dineshkrish.hibernate;

import org.hibernate.Session;

public class CustomerApplication {

	public static void main(String[] args) {
		// Defining Customer Object
		Customer customer = new Customer();
		customer.setCustomerName("Dinesh Krishnan");
		// Defining the Address Object
		Address customerAddress = new Address();
		customerAddress.setCity("New York");
		customerAddress.setState("New York");
		// Setting Address to Customer
		Session session = SessionProvider.getSession();
		// Saving the Customer Object to Database


Embedded and Embeddable Example


1. Hibernate 4.3 JavaDoc
2. JPA 2.1 Embeddable JavaDoc
3. JPA 2.1 Embedded JavaDoc