Hibernate - SPLessons

Hibernate Component Mapping

Home > Lesson > Chapter 22
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate Component Mapping

Hibernate Component Mapping

shape Description

Hibernate Component Mapping, When one class object is stored in another class, then that object is called a Hibernate Component Mapping objects. Hibernate Component Mapping object doesn't contain any primary key column, then go to has-A relationship. Mapping file, map the component object with component tag which is defined under the class tag. <component> is the element used in the mapping file of Hibernate.

shape Example

Hibernate Component Mapping, Take one Address class and Employee class, Address class doesn't contain any Primary key column, then the Address class object is created for Employee class using component Mapping relation.

shape Step 1

Create the project directory structure.

shape Step 2

Create the persistence class. Address.java [java]package com.itoolsinfo; public class Address { private String city, village, street; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getVillage() { return village; } public void setVillage(String village) { this.village = village; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } } [/java] Here the developer created the class Address and also given the variables city, village, street. Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them. Employee.java [java]package com.itoolsinfo; public class Employee { private int id; private String name; private Address address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } } [/java] Here the developer created the class Employee and also created the variables id, name, address. The developer used set and get methods to use the variables. Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them.

shape Step 3

To map the persistance classes in mapping file that is employee.hbm.xml. [xml]<?xml version='1.0' encoding='UTF-8'?>; <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN””http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”> <hibernate-mapping> <class name=”com.itoolsinfo.Employee”table=”employee”> <id name=”id”column=”id”> <generator class=”assigned”> </generator> </id> <property name=”name”length=”14”> <component name=”address” class=”com.itoolsinfo.Address” > <property name=”village” length=”14”/> <property name=”street” length=”14”/> <property name=”city” length=”14”> </property> </component> </class> </hibernate-mapping> [/xml] The generator class sub element of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface.

shape Step 4

Configure the Mapping file in Configuration file. hibernate.cfg.xml [xml] <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">system</property> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="employee.hbm.xml"/> </session-factory> </hibernate-configuration> [/xml]
Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.

shape Step 5

Create the class and perform the Database operations. SPLessons.java [java]package com.itoolsinfo; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class SPLessons { public static void main(String args[]) { Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession(); Address address1=new Address(); address1.setStreet("BN.Reddy nagar"); address1.setCity("hyderabad"); address1.setVillage("hariharapuram"); Employee employee=new Employee(); employee.setId(1); employee.setName(" Jhon "); employee.setAddress(address1); Transaction transaction=session.beginTransaction(); session.save(employee); transaction.commit(); session.close(); //String str=JOptionPane.showInputDialog("Enter name:user1"); //splessons.setName("str"); // transaction.commit(); // session.close(); // factory.close(); } } [/java] Here created the class SPLessons to perform data base operations on the available code. Application acquires session objects from Session Factory. SessionFactory is for the most part arranged as Singleton in application, SessionFactory stores produce SQL statements and other mapping metadata that Hibernate utilizes at runtime. Output: Output will be as follows, where enter the values into the columns. Data will be selected from the table as following command. [sql]select * from employee;[/sql] Tables will be created in the database as follows.

Summary

shape Key Points

  • Component mapping will be done on dependent object.
  • Component mapping will be use full at Has-A relation ship.
  • <component>is the element utilized in component mapping to work on dependent object.