ORM features in hibernate

Hibernate is Object Relational Mapping(ORM) framework. Hibernate has the advantages while comparing with JDBC. That advantages will make developer life will be easy were implementing or maintaining the java application. Hibernate ORM features are,
  • Hibernate gives the support of POJO/POJI model programming.
  • It is also API independent.
  • Light weight technology, no need of web or application servers.
  • No byte code enhancement like EJB.
  • Supports inheritance and polymorphism features. EJB component do not support inheritance.
  • Pluggable with any Java/JEE or framework software based applications.
  • The persistence logic of hibernate application can be access from all type of java client application.
  • Gives implicit middleware services like jdbc connection pooling, transaction management etc..,
  • Gives support to work with third party jdbc connection pooling.
  • Support to call stored procedures and functions.
  • Supports database independent query language called HQL[Hibernate Query Language].
  • Supports object level relationship.
                  1. One-to-One

                  2. One-to-Many
                  3. Many-to-One
                  4. Many-to-Many
  • Uses hibernate specific data structure "Bag" which is advanced to list.
  • The result generated via select operations is serializable object by default.
  • Provides three full-featured query facilities:
                 1. Hibernate Query Language
                 2. Hibernate Criteria Query API
                 3. Native SQL

Hibernate applications are not distributed. Hence the setup of hibernate must reside with the application/component which needs to interact with database software.

Note: Database server can be anywhere through out the world.Still our hibernate application can access our database server just like a jdbc program. But here entire setup should be there at client side as shown in the above diagram.

It's mandatory to have all resources like POJO classes, mapping files and configuration file along with client program in client side environment.

Below diagram gives you the clear cut picture which explains about clear picture,
  1. Server side environment
  2. Client side environment

Hibernate Configuration file:
  • Its a xml file.
  • Any file name *.xml can become hibernate configuration file.
  • Standard name is "hibernate.cfg.xml"
  • Database Level: It is a database level. It means for every database we can have a separate configuration file. Ex: If we are using only one database in our application then we can have only one hibernate configuration file. If we are using two databases in our application then need to use two hibernate configuration files and so on.
  • DTD[Document Type Definition]: In beginning of this file we need to specify DTD. It is used to validate xml file while parsing the xml file. It's compulsory to specify DTD. Instead of trying this DTD we can copy and paste it from existing predefined examples from 'hibernate-home\etc\hibernate.cfg.xml' file.
  • Then we need to configure all required properties of configuration file. Then we can go for configuring all the mapping files.
  • This file contains hibernate properties having values that are required to establish connection with database software and pass special instruction to database software and hibernate software.
  • Using these details hibernate software constructs a pool of hibernate session objects represented by hibernate session factory object.
  • Each hibernate session object encapsulates the functionality of jdbc connection object and statement object.
Properties of Hibernate Configuration file: In configuration file we need to configure the following 5 properties mandatory.
  • hibernate.dialect
  • hibernate.connection.driver_class(Oracle)/hibernate.connection.driver(MySQL)
  • hibernate.connection.url
  • hibernate.connection.username
  • hibernate.connection.password
Note: These properties need not to follow any order. Driver class, url, username and password are same like jdbc program.

Dialect class:
  • Its a database specific class. It means it differs from one database to another database.
  • Dialect  classes are provided by hibernate api.
  • Using dialect class our hibernate engine can know all the inner details about underlying database server. So that it can generate database specific queries at run time of program.
  • For MySQL database software: org.hibernate.dialect.MySQLDialect
  • for oracle 9i: org.hibernate.dialect.Oracle9Dialect
  • for other versions of oracle: org.hibernate.dialect.OracleDialect
There are many properties in hibernate configuration file. But most of the properties are having regularly used default values.

Hibernate mapping file:
  • Its a xml file.
  • Any file name *.xml can become hibernate mapping file.
  • Naming standard is <Pojo class name>.hbm.xml[hbm indecates hibernate mapping file]
  • DTD[Document Type Definition]: In beginning of this file we need to specify DTD. It is used to validate xml file while parsing the xml file. It is compulsory to specify DTD. DTD file of configuration file and mapping file are different.
  • One hibernate application can have multiple mapping files.
  • All mapping file names must be specified in hibernate.cfg.xml file.
  • We can have a separate mapping file for every pojo class or we can give mappings for multiple pojo classes in a single mapping file.
  • Hibernate mapping file contains OR Mapping configuration like class to table mapping members variables to columns mapping and relationships etc...
Types of Hibernate mappings: Hibernate allows the following OR Mapping file operations in mapping file.
  • Basic OR Mapping:[table-pojo class, table columns – pojo class properties...]
  • Inheritance Mapping:[<subclass>,<joined-subclass>...]
  • Collection Mapping: [<set>,<list>,<map>,<bag>,<idbag>]
  • Association Mapping: [<one-to-one>,<one-to-many>,<many-to-many>...]
  • Component Mapping: [<component> to declare another class object as a property of our pojo class].
Persistent Class [POJO Class]:
  • it is an ordinary java class without having API dependency.
  • An object of this class represents table row.
  • This class and its member variables are mapped with database table and its columns respectively in a hibernate mapping file.
  • Any java class can act as persistent class but it is recommended to take java bean as a persistent class with setter and getter methods.
Key points to remember about Hibernate Application:
  • It can be any Java/JEE application which needs to interact with database server.
  • The code in this application will be written by using hibernate API.
  • We need to follow following steps in our hibernate client application.
  1. Active hibernate engine.
  2. Read configuration file.
  • If hibernate configuration file is named as 'hibernate.cfg.xml' then need not to specify file name.
  • If hibernate configuration file is named other than 'hibernate.cfg.xml' then its compulsory to specify the file name.
  1. Build Session-factory.
  2. Get hibernate session object from session-factory.
  • Create persistent context object.
  • All persistent objects will be stored here.
  1. use session object to perform persistence operations through persistent class objects representing table rows.
  2. Close session object.
  • Release session object back to session-factory.
  • Destroy persistent context object. So that all persistent state objects will be thrown to detached state.
  1. close session-factory. It will destroy the entire session-factory so no more connection pool.
Note:
  • In the above 7 points, first 3 should get executed only once through out the application that too when we start to execute the application.
  • Point 4-6 are part of every client operation. So that every operation can interact with database server.
  • The final step should get executed only once at the time of terminating the application.
Synchronization: The modification what we do to out pojo class/persistent class will be reflected to table row and modification what we do for our table row will be reflected to pojo class object if the object is in persistent state.

How the hibernate program is able to perform synchronization between java object and table rows?
Ans: Every POJO class object that is representing table row will contain primary key value which is one of the number variables of the object. Based up on this primary key value synchronization works.

Hibernate software uses this primary key value as a criteria to update row data using select query and object data with row by using update query.

Note: Programmer configures one or more of the member variables of POJO class as primary key field in hibernate mapping file.

Primary Key: Every record of a table should be identified uniquely. To do this we can configure primary key.

Single field primary key: If only one member variable of POJO class is configured as primary key field then it is called singular field configuration. To configure this we can use <id> tag in mapping file.

Composite primary key: If more than one member variable of POJO class is configured as primary key field then it is called as composite primary key. To configure this we can use <composite-id> tag in mapping file.

If database table primary key is not specified then we can prefer unique key field to configure as a primary key in hibernate mapping file.

Different states of POJO class object: there are 3 states which are as follows,
1. Transient state:
  • Object is created by programmer with data. But is doesn't represent any table row.
  • This object does not contains any primary key value.
  • The object which is created for POJO class and which is not under control of hibernate application resides in transient state.
2. Persistent state:
  • The object that represents table row with primary key and managed under control of hibernate software is called as persistent state.
  • This object will be in synchronization with table row. 
  • Hibernate application developer's uses this kind of object in persistent logic development.
3. Detached state:
  • when session is closed persistent context will be destroyed and all the objects which are in persistent state will be thrown to detached state.
  • when table row of persistent state object is deleted then object becomes detached state.

Comments

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example