Over a million developers have joined DZone.

Hibernate Component Mapping Using Annotations Tutorial

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

In this example you will learn how to map components using Hibernate Annotations. Consider the following relationship between Student and Address entity.

According to the relationship each student should have a unique address.

Since the Student and Address entities are strongly related (composition relation), it is better to store them in a single table. The relational model is shown below.

Student class is used to create the STUDENT table.

package com.vaannila.student;

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

@Table(name = "STUDENT")
public class Student {

	private long studentId;
	private String studentName;
	private Address studentAddress;

	public Student() {

	public Student(String studentName, Address studentAddress) {
		this.studentName = studentName;
		this.studentAddress = studentAddress;

	@Column(name = "STUDENT_ID")
	public long getStudentId() {
		return this.studentId;

	public void setStudentId(long studentId) {
		this.studentId = studentId;

	@Column(name = "STUDENT_NAME", nullable = false, length = 100)
	public String getStudentName() {
		return this.studentName;

	public void setStudentName(String studentName) {
		this.studentName = studentName;

	public Address getStudentAddress() {
		return this.studentAddress;

	public void setStudentAddress(Address studentAddress) {
		this.studentAddress = studentAddress;


The @Embedded annotation is used to specify the Address entity should be stored in the STUDENT table as a component.

@Embeddable annotation is used to specify the Address class will be used as a component. The Address class cannot have a primary key of its own, it uses the enclosing class primary key.

package com.vaannila.student;

import javax.persistence.Column;
import javax.persistence.Embeddable;

public class Address {

	private String street;
	private String city;
	private String state;
	private String zipcode;

	public Address() {

	public Address(String street, String city, String state, String zipcode) {
		this.street = street;
		this.city = city;
		this.state = state;
		this.zipcode = zipcode;
	@Column(name = "ADDRESS_STREET", nullable = false, length=250)
	public String getStreet() {
		return this.street;

	public void setStreet(String street) {
		this.street = street;

	@Column(name = "ADDRESS_CITY", nullable = false, length=50)
	public String getCity() {
		return this.city;

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

	@Column(name = "ADDRESS_STATE", nullable = false, length=50)
	public String getState() {
		return this.state;

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

	@Column(name = "ADDRESS_ZIPCODE", nullable = false, length=10)
	public String getZipcode() {
		return this.zipcode;

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;


Now create the hibernate configuration file and add the Student and Address classes.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url"> jdbc:hsqldb:hsql://localhost</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="connection.password"></property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create-drop</property>
        <mapping class="com.vaannila.student.Student" />
        <mapping class="com.vaannila.student.Address" />

Create the Main class to run the example.

package com.vaannila.student;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.vaannila.util.HibernateUtil;

public class Main {

	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Address address = new Address("OMR Road", "Chennai", "TN", "600097");
			Student student = new Student("Eswar", address);
		} catch (HibernateException e) {
		} finally {



On executing the Main class you will see the following output.

Each student has one address and the values are stored in the same STUDENT table.

The folder structure of the example is shown below.

You can download the source code of this example here.

Source :Download

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}