Recent Posts

Sunday, 15 May 2016

Hibernate Tutorial

Where actually Java is used?
1. Java is used to develop the enterprise applications
2. Enterprise means business organization
3. Business organisation provides services
4. Computerising business services is called Enterprise Application. Following picture depicts the architecture of the Enterprise Application.

Client Layer
     It is browser software.

Controller Layer
1. Receiving user request from client (calling request)
2. Capturing the user provided data
3. Validating the user input 
4. Calls the business method to get business services and get processed data
5. Keep the processed data in memory(request/session/application scope) 
6. Finally forwarding the request to VIEW.

     Should not write business-Logic/Data-Access-Logic in the Controller Layer. Because that is not reusable.

Presentation Layer
1. Receive the control from Controller Layer
2. Generate the output by taking the data from memory(request/session/application scope), which is stored by controller layer
3. Generated output will be given to web-server, which intern return present the output to browser

Business/Service Layer
1. Receiving request from Controller Layer
2. Contacting the Data Access Layer to get the database data
3. Implementing the business logic
4. Return the control/processed-data to Controller Layer

Persistent/Data-Access Layer
1. Receiving the request from business Layer
2. Contacting database to get the database data
3. Return the accessed data to business Layer

Data Layer
     It is a database.

Different logic's available in Enterprise Application
Presentation Logic
     Logic used to present the output/input.
Application/Controlling Logic
     Logic used to control the flow of application.
Business Logic
     Programmatical implementation of business rules is nothing but business logic.
Data Access Logic
     Logic used to contact the Database.

Sun Micro systems technologies and frameworks in enterprise application development    

 Non-Sun Micro systems technologies and frameworks in enterprise application development

     The permanent storage unit that can store data is called Persistence Store. 
E.g: Files, Database software etc.

     Insert, update, select and delete operations are performed on the Database software (Persistence Store). The process of storing enterprise data in to database is known as persistence. The logic used for this purpose is known as persistence logic. Persistence operations also called as CURD or CRUD or SCUD operations.

Here C = Create, 
         U = Update, 
         R/S = Read/Select
         D = Delete

    Files are suitable as persistence store in small scale applications like desktop and mobile applications etc. DB software's are suitable as persistence store in medium and large scale applications like websites, bank applications etc.

     DB software's that is capable of storing objects as table column values is known as object DB (ODB) software's.

Limitations with ODB Software's
1. Storing multiple values in a single column of ODB table is against the Normalisation rule.
2. Generating the reports from table of ODB software is complex process.

Limitations with Files
1. No security
2. Can't store huge amount of data.
3. No query language support.
4. Manipulations are very complex.
5. Merging and comparison of data is very complex.

     To solve problems with files and ODB software's use RDBMS software's like oracle, MySQL etc as persistence store. Java application can use JDBC code as persistence logic to interact with RDBMS database software's but following limitations are there

1. SQL queries are database dependent queries since JDBC persistence logic use SQL queries. The JDBC code is database dependent persistence logic due to this changing database software in the middle of the production environment and in the middle of development environment is complex process.
2. JDBC ResultSet object is not serializable object so that we can't send object directly over the network.
3. Exception handling, transaction management facilities in JDBC coding in JDBC coding are not that much sufficient in large scale application development.

     To solve all these problems use ORMapping based persistence logic to interact with RDBMS DB software's. 

     The process of mapping Java class with DB table, member variables with table columns and making that java class objects representing DB table rows by having synchronization between them is called ORMapping

* Synchronization between object and table row is nothing but modification done in java object will reflect in table row and vice versa. 
* ORM software's are responsible for this synchronization and to develop objects based ORMapping persistence logic.
* In ORMapping Java class with DB table, member variables with table columns and making that java class objects representing DB table rows and there is no need of SQL queries. This makes ORMapping persistence logics as DB independent persistence logic.
* Due to this, we can change DB software in middle of project development or utilization without disturbing the persistence logic.
class Employee {
   String empName;
   int empAge;
   String empAddress;

     All ORM software's internally use JDBC code to perform persistence operation on DB table rows.

What is Hibernate
     Hibernate is an opensource, light weight pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables. The application that deal with complex and heavy weight business logic and contains additional services like security, transaction management and etc is called an enterprise application.

What is a framework?
     A framework is reusable semi finished application that can be customized to develop a specific application.

Feature's of hibernate
* Hibernate persists java objects into database (Instead of primitives)
* Hibernate generates efficient queries for java application to communicate with Database
* It provides fine-grained exception handling mechanism. In hibernate we only have Un-checked exceptions, so no need to write try, catch, or no need to write throws (In hibernate we have the translator which converts checked to Un-checked)
* It supports synchronization between in-memory java objects and relational records
* Hibernate provides implicit connection pooling mechanism
* Hibernate supports a special query language(HQL) which is Database vendor independent
* Hibernate has capability to generate primary keys automatically while we are storing the records into database
* Hibernate addresses the mismatches between java and database
* Hibernate provides automatic change detection
* Database objects (tables, views, procedures, cursors, functions ... etc) name changes will not affect hibernate code
* Supports over 30 dialects
* Lazy loading concept is also included in hibernate so you can easily load objects on start up time
* Getting pagination in hibernate is quite simple.
* Hibernate Supports automatic versioning of rows
* Hibernate supports Inheritance, Associations, Collections
* Hibernate provides caching mechanism for efficient data retrieval
* Hibernate provides transactional capabilities that can work with both stand-alone or java Transaction API (JTA) implementations etc
* Hibernate supports annotations, apart from XML

Disadvantages of hibernate
* Since hibernate generates lots of SQL statements at runtime so it is slower than pure JDBC
* Hibernate is not much flexible in case of composite mapping. This is not disadvantage since understanding of composite mapping is complex
* Hibernate does not support some type of queries which are supported by JDBC
* Boilerplate code issue, actually we need to write same code in several files in the same application, but spring eliminated this

Hibernate Architecture

     Java Application makes use of hibernate API methods calls to inform the persistent needs to hibernate. Then Hibernate engine generate JDBC code that corresponds to the underlying DB by using mapping file and configuration file information. We can also find the architecture diagrams as follows

Hibernate mapping file
* In this file hibernate application developer specify the mapping from entity class name to table name and entity properties names to table column names. i.e. mapping object oriented data to relational data is done in this file
* Standard name for this file is <domain-object-name.hbm.xml> 
* In general, for each domain object we create one mapping file
Number of Entity classes = that many number of mapping xmls

* Mapping can be done using annotations also. If we use annotations for mapping then we no need to write mapping file.
* From hibernate 3.x version on wards it provides support for annotation, So mapping can be done in two ways
                                                                   1. XML
                                                                   2. Annotations

Syntax Of Mapping xml
   <class name="POJO class name" table="table name in database">
      <id name="variable name" column="column name in database" type="java/hibernate type" />
      <property name="variable1 name" column="column name in database" type="java/hibernate type" />
      <property name="variable2 name" column="column name in database" type="java/hibernate type" />
Hibernate configuration file
* It is an XML file in which database connection details (username, password, url, driver class name) and , Hibernate Properties(dialect, show-sql, second-level-cache ... etc) and Mapping file name(s) are specified to the hibernate
* Hibernate uses this file to establish connection to the particular database server 
* Standard name for this file is <hibernate.cfg.xml>
* We must create one configuration file for each database we are going to use, suppose if we want to connect with 2 databases, like Oracle, MySql, then we must create 2 configuration files. 
No. of databases we are using = That many number of configuration files

* We can write this configuration in 2 ways ...
                                                              1. XML file
                                                              2. Properties file(o1d style)

* We don't have annotations to write configuration details. Actually in hibernate l.x, 2.x we defined this configuration by using .properties file, but from 3.x XML came into picture. XML files are always recommended to use.

Syntax Of Configuration xml
      <!-- Related to the connection START -->
      <property name="connection.driver_class">Driver Class Name </property>
      <property name="connection.url">URL </property>
      <property name="connection.user">user </property>
      <property name="connection.password">password</property>
      <!-- Related to the connection END -->

      <!-- Related to hibernate properties START -->
      <property name="show_sql">true/false</property>
      <property name="dialet">Database dialet class</property>
      <property name="">create/update or what ever</property>
      <!-- Related to hibernate properties END-->

      <!-- Related to mapping START-->
      <mapping resource="hbm file 1 name .xml" / >
      <mapping resource="hbm file 2 name .xml" / >
      <!-- Related to the mapping END -->
Simple Hibernate Application Requirements
1. Entity class
2. Mapping file(Required if you are not using annotations)
3. Configuration file
4. DAO class (Where we write our logic to work with database)

Next Tutorial  Setting Hibernate Environment

1 comment: