Pages

Thursday, October 14, 2010

Hibernate Introduction

  















Advantages of Hibernate over JDBC

1) Hibernate is data base independent, same code will work for all data bases like ORACLE,MySQL ,SQLServer etc.
In case of JDBC query must be data base specific.

2) As Hibernate is set of Objects , you don't need to learn SQL language.
You can treat TABLE as a Object .
In case of JDBC you need to learn SQL.

3) Don't need Query tuning in case of Hibernate. If you use Criteria Quires in Hibernate then hibernate automatically tuned your query and return best result with performance.
In case of JDBC you need to tune your queries.

4) You will get benefit of Cache. Hibernate support two level of cache. First level and 2nd level. So you can store your data into Cache for better performance.
In case of JDBC you need to implement your java cache .


 
5) Hibernate supports Query cache and It will provide the statistics about your query and database status.
JDBC Not provides any statistics.

6) Development fast in case of Hibernate because you don't need to write queries.

7) No need to create any connection pool in case of Hibernate. You can use c3p0.
In case of JDBC you need to write your own connection pool.

8) In the xml file you can see all the relations between tables in case of Hibernate. Easy readability.

9) You can load your objects on start up using lazy=false in case of Hibernate.
JDBC Don't have such support.


10 ) Hibernate Supports automatic versioning of rows but JDBC Not.
Portable Relationships

The name ORM (Object Relation Mapping) actaully came because of Portable relationships feature.

This feature says dont maintain relationship between tables instead maintain relationship between objects.

If the relationship between tables are created, during shifting the relationships need to be created again on newly shifted database, instead if relationships are maintained objects, relations become portable to all the DB's. DB is only intend to store and retrieve data but not to maintain relationship between tables.

ORM says dont insert record and retrieve record from DB. Store java class object into ORM that converts objects as a record and inserts into database, ORM retrives record from database and stores into java class object and returns to client.

********** ORM says deal with objects but not with table **********


Hibernate Configuration File (hibernate.cfg.xml)

Hibernate says instead of developer creating database connection objects, hibernate will automatically create connection objects and perform SQL operation on database.

For that Hibernate requires Driver class name, URL, user name and password. These properties must be applied in hibernate.cfg.xml file.

With these properties Hibernate will create database connection during session object.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

In the above configuration file we specified to use the "hibernatetutorial" which is running on localhost and the user of the database is root with no password. The dialect property  is org.hibernate.dialect.MySQLDialect which tells the Hibernate that we are using MySQL Database. Hibernate supports many database. With the use of the Hibernate (Object/Relational Mapping and Transparent Object Persistence for Java and SQL Databases),  we can use the following databases dialect type property:

DB2                                 - org.hibernate.dialect.DB2Dialect
HypersonicSQL                - org.hibernate.dialect.HSQLDialect
Informix                          - org.hibernate.dialect.InformixDialect
Ingres                              - org.hibernate.dialect.IngresDialect
Interbase                         - org.hibernate.dialect.InterbaseDialect
Pointbase                        - org.hibernate.dialect.PointbaseDialect
PostgreSQL                      - org.hibernate.dialect.PostgreSQLDialect
Mckoi SQL                        - org.hibernate.dialect.MckoiDialect
Microsoft SQL Server       - org.hibernate.dialect.SQLServerDialect
MySQL                              - org.hibernate.dialect.MySQLDialect
Oracle (any version)         - org.hibernate.dialect.OracleDialect
Oracle 9                            - org.hibernate.dialect.Oracle9Dialect
Progress                            - org.hibernate.dialect.ProgressDialect
FrontBase                         - org.hibernate.dialect.FrontbaseDialect
SAP DB                              - org.hibernate.dialect.SAPDBDialect
Sybase                              - org.hibernate.dialect.SybaseDialect
Sybase Anywhere             - org.hibernate.dialect.SybaseAnywhereDialect

The <mapping resource="contact.hbm.xml"> property is the mapping for our contact table.

show_sql :

If it is true, We can see the Hibernate generated query in console.
If it is false, It does not show the generated query in the console.

hbm2ddl:

If the value is create, it will drop the old table and it will recreate the table.
If the value is update and there is no table, Then it creates the table and if there is a table, it will do SQL operation.

No comments:

Post a Comment