Hibernate > Hibernate Mapping Component

Hibernate Mapping Component

In this example you will learn how to map components using Hibernate. 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.hbm.xml is used to create the STUDENT table.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	<class name="com.w3javaguide.student.Student" table="STUDENT">
		<meta attribute="class-description">This class contains student details.</meta>
		<id name="studentId" type="long" column="STUDENT_ID">
			<generator class="native" />
		<property name="studentName" type="string" not-null="true"
			column="STUDENT_NAME" />
		<component name="studentAddress" class="com.w3javaguide.student.Address">
			<property name="street" column="ADDRESS_STREET" type="string"
				length="250" />
			<property name="city" column="ADDRESS_CITY" type="string"
				length="50" />
			<property name="state" column="ADDRESS_STATE" type="string"
				length="50" />
			<property name="zipcode" column="ADDRESS_ZIPCODE" type="string"
				length="10" />

The component element is used to map all the Address entity fields to the STUDENT table. In Hibernate terms the Address entity is called the component and it cannot have its own primary key, it uses the primary key of the enclosing Student entity.

Now create the hibernate configuration file.

<?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="">create-drop</property>
        <mapping resource="com/w3javaguide/student/Student.hbm.xml"/>

After creating the configuration file, generate java class files using Hibernate Tools.(To generate code using Hibernate Tools refer this example )

The following classes will be generated. You will still have a Student and Address class seperately but they will be mapped to only one table.

package com.w3javaguide.student;

// Generated Sep 3, 2012 6:57:20 PM by Hibernate Tools 3.2.4.GA

 * This class contains student details.
public class Student implements {

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

	public Student() {

	public Student(String studentName) {
		this.studentName = studentName;

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

	public long getStudentId() {
		return this.studentId;

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

	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;


package com.w3javaguide.student;

// Generated Sep 3, 2012 6:57:20 PM by Hibernate Tools 3.2.4.GA

 * This class contains student details.
public class Address implements {

	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; = city;
		this.state = state;
		this.zipcode = zipcode;

	public String getStreet() {
		return this.street;

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

	public String getCity() {

	public void setCity(String city) { = city;

	public String getState() {
		return this.state;

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

	public String getZipcode() {
		return this.zipcode;

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


Create the Main class to run the example.

package com.w3javaguide.student;

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

import com.w3javaguide.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("arandev", 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
Contact Us  |  Copyright © 2012