Component Mapping

Component Mapping

In Hibernate applications, Component Mapping will provide solution for Granularity Mismatch.

In Component mapping, we will define a component property in order to refer multiple columns in a Database table.

To configure component property in hibernate applications we will use the following XML tags.

<hibernate-mapping>
   <class name="--" table="--">
      -------
      <component name="--" class="--">
          -----
      </component>
      -------
   </class>
</hibernate-mapping>

Where “<component>” tag will take Component class and its reference variable declaration, where “name” attribute will take component property reference variable and “class” attribute will take fully qualified name of the respective component class.

E.g

Account.java

package com.ashok.hibernate.model;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
	private String accNo;
	private String accName;
	private String accType;

	public String getAccNo() {
		return accNo;
	}

	public void setAccNo(String accNo) {
		this.accNo = accNo;
	}

	public String getAccName() {
		return accName;
	}

	public void setAccName(String accName) {
		this.accName = accName;
	}

	public String getAccType() {
		return accType;
	}

	public void setAccType(String accType) {
		this.accType = accType;
	}
}

Address.java

package com.ashok.hibernate.model;

public class Address {
	private String pno;
	private String street;
	private String city;

	public String getPno() {
		return pno;
	}

	public void setPno(String pno) {
		this.pno = pno;
	}

	public String getStreet() {
		return street;
	}

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

	public String getCity() {
		return city;
	}

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

Employee.java

package com.ashok.hibernate.model;

public class Employee {
	private int eno;
	private String ename;
	private float esal;
	private Account eacc;
	private Address eaddr;

	public int getEno() {
		return eno;
	}

	public void setEno(int eno) {
		this.eno = eno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public float getEsal() {
		return esal;
	}

	public void setEsal(float esal) {
		this.esal = esal;
	}

	public Account getEacc() {
		return eacc;
	}

	public void setEacc(Account eacc) {
		this.eacc = eacc;
	}

	public Address getEaddr() {
		return eaddr;
	}

	public void setEaddr(Address eaddr) {
		this.eaddr = eaddr;
	}
}

employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.ashok.hibernate.model.Employee" table="emp">
		<id name="eno" />
		<property name="ename" />
		<property name="esal" />
		<component name="eacc" class="com.ashok.hibernate.model.Account">
			<property name="accNo" />
			<property name="accName" />
			<property name="accType" />
		</component>
		<component name="eaddr" class="com.ashok.hibernate.model.Address">
			<property name="pno" />
			<property name="street" />
			<property name="city" />
		</component>
	</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <mapping resource="com/ashok/hibernate/employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>

ClientApp.java

package com.ashok.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {
		SessionFactory sessionFactory = null;
		Session session = null;
		try {
			Configuration cfg = new Configuration();
			cfg.configure();
			StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
			builder = builder.applySettings(cfg.getProperties());
			StandardServiceRegistry registry = builder.build();
			sessionFactory = cfg.buildSessionFactory(registry);
			session = sessionFactory.openSession();
			Account acc = new Account();
			acc.setAccNo("123456789");
			acc.setAccName("Ashok Kumar");
			acc.setAccType("Savings");
			Address addr = new Address();
			addr.setPno("9000027370");
			addr.setStreet("Nijampet");
			addr.setCity("Hyderabad");

			Employee emp = new Employee();
			emp.setEno(87);
			emp.setEname("Ashok Kumar");
			emp.setEsal(50000);
			emp.setEacc(acc);
			emp.setEaddr(addr);
			Transaction tx = session.beginTransaction();
			session.save(emp);
			tx.commit();
			System.out.println("Employee Inserted Successfully");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != session) {
				session.close();
			}
			if (null != sessionFactory) {
				sessionFactory.close();
			}
		}
	}
}

To provide component mapping in Hibernate applications we will use the following annotations from javax.persistence package.

  • @Embeddable
  • @Embedded
  • Where @Embeddable annotation will be used over the Component class.
  • Where @Embedded annotation will be used over the component property.

Example

Account.java

package com.ashok.hibernate;

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

/**
 * 
 * @author Ashok Kumar
 *
 */
@Entity
@Embeddable
public class Account {
	@Column(name="ACCNO")
	private String accNo;
	@Column(name="ACCNAME")
	private String accName;
	@Column(name="ACCTYPE")
	private String accType;

	public String getAccNo() {
		return accNo;
	}

	public void setAccNo(String accNo) {
		this.accNo = accNo;
	}

	public String getAccName() {
		return accName;
	}

	public void setAccName(String accName) {
		this.accName = accName;
	}

	public String getAccType() {
		return accType;
	}

	public void setAccType(String accType) {
		this.accType = accType;
	}
}
package com.ashok.hibernate;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
/**
 * 
 * @author ashok.mariyala
 *
 */
@Entity
@Embeddable
public class Address {
	@Column(name="PNO")
	private String pno;
	@Column(name="STREET")
	private String street;
	@Column(name="CITY")
	private String city;

	public String getPno() {
		return pno;
	}

	public void setPno(String pno) {
		this.pno = pno;
	}

	public String getStreet() {
		return street;
	}

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

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}
}
package com.ashok.hibernate;

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

/**
 * 
 * @author ashok.mariyala
 *
 */
@Entity
@Table(name="emp")
public class Employee {
	@Id
	@Column(name="ENO")
	private int eno;
	@Column(name="ENAME")
	private String ename;
	@Column(name="ESAL")
	private float esal;
	@Embedded
	private Account eacc;
	@Embedded
	private Address eaddr;

	public int getEno() {
		return eno;
	}

	public void setEno(int eno) {
		this.eno = eno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public float getEsal() {
		return esal;
	}

	public void setEsal(float esal) {
		this.esal = esal;
	}

	public Account getEacc() {
		return eacc;
	}

	public void setEacc(Account eacc) {
		this.eacc = eacc;
	}

	public Address getEaddr() {
		return eaddr;
	}

	public void setEaddr(Address eaddr) {
		this.eaddr = eaddr;
	}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <mapping resource="com/ashok/hibernate/insert/employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
package com.ashok.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {
		SessionFactory sessionFactory = null;
		Session session = null;
		try {
			Configuration cfg = new Configuration();
			cfg.configure();
			StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
			builder = builder.applySettings(cfg.getProperties());
			StandardServiceRegistry registry = builder.build();
			sessionFactory = cfg.buildSessionFactory(registry);
			session = sessionFactory.openSession();
			Employee emp = (Employee) session.get("com.ashok.hibernate.model.Employee.class", 111);
			System.out.println("Employee Details");
			System.out.println("------------------------");
			System.out.println("Employee Number :" + emp.getEno());
			System.out.println("Employee Name :" + emp.getEname());
			System.out.println("Employee Salary :" + emp.getEsal());
			System.out.println();
			Account acc = emp.getEacc();
			System.out.println("Account Details");
			System.out.println("-------------------");
			System.out.println("Account Number :" + acc.getAccNo());
			System.out.println("Account Name :" + acc.getAccName());
			System.out.println("Account Type :" + acc.getAccType());
			System.out.println();
			Address addr = emp.getEaddr();
			System.out.println("Address Details");
			System.out.println("--------------------");
			System.out.println("PNO :" + addr.getPno());
			System.out.println("Street :" + addr.getStreet());
			System.out.println("City :" + addr.getCity());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != session) {
				session.close();
			}
			if (null != sessionFactory) {
				sessionFactory.close();
			}
		}
	}
}
Component Mapping
Scroll to top