Nearby lessons

18 of 19

Hibernate - Different Types of Mappings

📌 What You Will Learn
  • Understand Hibernate - Different Types of Mappings
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Different Types of Mappings step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Different Types of Mappings

  • List<Object[]> list = c.list();
  • System.out.println("ENO\tENAME\tESAL\tEADDR");
  • System.out.println("-------------------------------");
  • for(Object[] obj: list) {
  • for(Object o: obj) {
  • System.out.print(o+"\t");
  • System.out.println();
  • session.close();
  • sessionFactory.close();

Hibernate Filters

In Hibernate Applications, by using HQL queries, we are able to retrive multiple records of data from database table. While retriving multiple Records from Database table if we want to filter the results on the basis of a particular condition then we have to use "Hibernate Filters".

If we want to use Filters in Hibernate applications we have to use the following steps.

1)Prepare a table in Database with the required columns, where we must

define a filter parameter column

sql>create table emp(ENO number primary key, ENAME varchar2(10), ESAL float, EADDR varchar2(10), ETYPE varchar2(10));

Here "ETYPE" is Filter parameter column, by using this Filter parameter column only we are able to prepare Filter condition inorder to filter the results.

Define Filte in mapping File

To define Filter in mapping file we have to use the following xml tags in mapping file.

  • <hibernate-mapping>
  • <class name="--" table="--">
  • <filter name="--" condition="--"/>
  • </class>
  • <filter-def name="--">
  • <filter-param name="--" type="--"/>
  • </filter-def>
  • </hibernate-mapping>

 Where <filder-def> tag can be used to define a filter in Hibernate mapping file.  Where "name" attribute in <filter-def> tag is able to take logical name of the filter.  Where <filter-param> tag is a chaild tag to <filter-def> tag , it will define a particular filter

parameter.  Where "name" attribute in <filter-param" tag will take name of the filter parameter.  Where "type" attribute in <filter-param> tag will take java type like "string", "byte", "int",....

 Where "<filter>" tag under <class> tag is able to configure Filter to mapping.  Where "name" attribute in <filter> tag is able to take logical of the filter which we have

defined in <filter-def>" tag.  Where "condition" attribute in <filter> tag is able to define condition inorder to filter the

results. 3) Enable Filter in Session and set values to filter parameters inorder to filter

the results — Employee.java

To enable Filter in Session we have to use the following method from org.hibernate.Session. public Filter enableFilter(String logical_name)

To set values to the Filter parameter we will use the following method. public void setParameter(String filter_Name, xxx value) Where xxx may be byte, short, int,.....

To disable Filter , we will use the following method from org.hibernate.Session. public void disableFilter(String logical_Name)

Example:

Example05
Loading editor…

the results — Employee.hbm.xml

Example06
Loading editor…

the results — hibernate.cfg.xml

Example07
Loading editor…

the results — Test.java

Example08
Loading editor…

Hibernate Mappings

In Enterprise Applications, we are able to use the data models like Object Oriented Data Model, Relational Data Model,...In general, Front-End applications are able to use Object Oriented Data Model to represent data and Back-End Systems are able to use Relational Data Model to represent data.

In the above context, both Object Oriented Data Model and relational Data model are having their own conventions to represent data, these different conventions will create mismatches between data models, it will reduce data persistency in enterprise applications.

In the above context, to improve data persistency in enterprise applications we have to solve the mismatches between data models, for thos we have to use ORM implementations.

Hibernate is one of the ORM implementation , it has provided "Hibernate Mappings" feature to resolve mismatches between data models. To resolve mismatches between data models, Hibernate has provided the following mappings.

Different Types of Mappings

  • Basic OR Mapping.
  • Component Mapping
  • Inheritance Mapping
  • Association Mapping

Basic OR Mapping

It is normal mapping between a class, an ID property, a normal property from Object Oriented data model and a table, a primary key column and normal columns.

Component Mapping — Account.java

IN Hibernate applications, Component Mapping will provide solution for Granualarity Mismatch.

In Component mapping, we will define a component property inorder tyo 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.

Example:

Example12
Loading editor…

Component Mapping — Address.java

Example13
Loading editor…

Component Mapping — Employee.java

Example14
Loading editor…

Component Mapping — Employee.hbm.xml

Example15
Loading editor…

Component Mapping — hibernate.cfg.xml

Example16
Loading editor…

Component Mapping — Test.java

Example17
Loading editor…

Component Mapping — Account.java

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 classs.  Where @Embedded annotation will be used over the component property.

Example:

Example18
Loading editor…

Component Mapping — Address.java

Example19
Loading editor…

Component Mapping — Employee.java

Example20
Loading editor…

Component Mapping — hibernate.cfg.xml

Example21
Loading editor…

Component Mapping — Test.Java

Example22
Loading editor…

Inheritance Mapping — Account.java

In Enterprise Applications, we are able to use the data models like Object Oriented Data Model, Relational Data Model,...In general, Front-End applications are able to use Object Oriented Data Model to represent data and Back-End Systems are able to use Relational Data Model to represent data. In Object Oriented Data Model, we will provide inheritance relation between entities inorder to improve code Reusability.

In Relational data Model, we will use different approaches to manage inheritance kind of features at databases like maintaining all the classes properties in a single table or defining a seperate table for each and every class and providing PK-FK relation between these tables,.....

In both the above cases, approaches are different to achive inheritance kind of feature, it will provide Sub types mismatches between data models, it will reduce data persistencey in enterprise applications.

In the above context, to improve data persistency in enterprise applications we have to use ORM implementation, Hibernate is one of the ORM implementation, it has provided the following three strategies to resolve inheritance mismatch.

  • Table Per Class Hierarchy.
  • Table Per Sub Class
  • Table Per Concreate Class

1) Table Per Class Hierarchy. In Table Per Class hierarchy, we will prepare a table with the columns representing all the properties of the classes[sub classes and super class] which are existed in inheritance.

In this mechanism, when we store any sub class object then data must be stored in single table in the respective columns, if data is not available for any column then null values will be stored in the respective columns.

If we want to use Table Per Class Hierarchy in Hibernate applications then we have to use the following steps. 1) Prepare all java classes which we want to keep in Inheritance

EX:

Example23
Loading editor…

Inheritance Mapping — StudentAccount.java

Example24
Loading editor…

Inheritance Mapping — EmployeeAccount.java

Example25
Loading editor…

Inheritance Mapping

2) Create a table in Database with a set of columns which are representinig all properties of

the classes which are existed in inheritance and discriminator column

SQL> create table account(ACCNO varchar2(5) primary key, ACCNAME varchar2(5), accType varchar2(10), SID varchar2(5), SBRANCH varchar2(5), SMARKS number(3), EID varchar2(5), ESAL float, EADDR varchar2(10), TYPE varchar2(10));

Where the purponse Discriminator column is to specify which sub class object data is represented by the present record.

3) Provide all subclasses configuration and discriminator column configuration in

hibernate mapping file by using the following tags

  • <hibernate-mapping>
  • <class name="--" class="--">
  • <discrimantor column="--"/>
  • <subclass name="--" discriminator-value="--">
  • </subclass>
  • </class>
  • </hibernate-mapping>

 Where "<discriminator>" tag can be used to configure discriminator column, where

"column" attribute in <discriminator> tag will take the discriminator column name which we defined in database table.

 Where "<subclass> tag is able to configure a single sub class and its properties, where "name" attribute will take fully qualified name of the respective sub class, where "discriminator-value" attribute will provide the exact discriminator value inorder to insert or retrive sub class object data.

Example28
Loading editor…

hibernate mapping file by using the following tags

4) In client application, Create Sub class objects with the data and perform the required

database operation

  • SessionFactory factory = cfg.buildSessionFactory(registry);
  • Session session = factory.openSession();
  • StudentAccount sa = new StudentAccount();
  • sa.setAccNo("a1");
  • sa.setAccName("AAA");
  • sa.setAccType("Savings");
  • sa.setSid("S1");
📝 Key Takeaways
  • Key ideas of Hibernate - Different Types of Mappings explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.