Sunday, February 1, 2009

Hibernate





  • Hibernate is not the best solutions for data centric applications that only uses the stored-procedures to implement the business logic in database.


  • It is most useful with object-oriented domain modes and business logic in the Java-based middle-tier.


  • Hibernate allows transparent persistence that enables the applications to switch any database.


  • Hibernate allows transparent persistence that enables the applications to switch any database


Features of Hibernate:





  • Hibernate 3.0 provides three full-featured query facilities:




  1. Hibernate Query Language,


  2. The newly enhanced Hibernate Criteria Query API, and


  3. Enhanced support for queries expressed in the native SQL dialect of the database.




  • Filters for working with temporal (historical), regional or permissioned data.


  • Enhanced Criteria query API: with full support for projection/aggregation and subselects.


  • Enhanced Criteria query API: with full support for projection/aggregation and subselects.


  • Eclipse support, including a suite of Eclipse plug-ins for working with Hibernate 3.0, including mapping editor, interactive query prototyping, schema reverse engineering tool.


  • Hibernate is Free under LGPL: Hibernate can be used to develop/package and distribute the applications for free.


  • Hibernate is Scalable: Hibernate is very performant and due to its dual-layer architecture can be used in the clustered environments.


  • Less Development Time: Hibernate reduces the development timings as it supports inheritance, polymorphism, composition and the Java Collection framework.


  • Automatic Key Generation: Hibernate supports the automatic generation of primary key for your.


  • JDK 1.5 Enhancements: The new JDK has been released as a preview earlier this year and we expect a slow migration to the new 1.5 platform throughout 2004. While Hibernate3 still runs perfectly with JDK 1.2, Hibernate3 will make use of some new JDK features. JSR 175 annotations, for example, are a perfect fit for Hibernate metadata and we will embrace them aggressively. We will also support Java generics, which basically boils down to allowing type safe collections


  • EJB3-style persistence operations: EJB3 defines the create() and merge() operations, which are slightly different to Hibernate's saveOrUpdate() and saveOrUpdateCopy() operations. Hibernate3 will support all four operations as methods of the Session interface.


  • Hibernate XML binding enables data to be represented as XML and POJOs interchangeably.


  • The EJB3 draft specification support for POJO persistence and annotations.




Hibernate Architecture

Hibernate architecture has three main components:


  • Connection Management

Hibernate Connection management service provide efficient management of the database connections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection.



  • Transaction management:

Transaction management service provide the ability to the user to execute more than one database statements at a time.



  • Object relational mapping:

Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update and delete the records form the underlying table. When we pass an object to a Session.save() method, Hibernate reads the state of the variables of that object and executes the necessary query.


Hibernate is very good tool as far as object relational mapping is concern, but in terms of connection management and transaction management, it is lacking in performance and capabilities. So usually hibernate is being used with other connection management and transaction management tools. For example apache DBCP is used for connection pooling with the Hibernate.
Hibernate provides a lot of flexibility in use. It is called "Lite" architecture when we only uses the object relational mapping component. While in "Full Cream" architecture all the three component Object Relational mapping, Connection Management and Transaction Management) are used.


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



Hibernate's Session


Hibernate Session is the main runtime interface between a Java application and Hibernate. First we are required to get the Hibernate Session.SessionFactory allows application to create the Hibernate Sesssion by reading the configuration from hibernate.cfg.xml file. Then the save method on session object is used to save the contact information to the database:
session.save(contact)

Here are the list of some commonly used generators in hibernate:

Increment
It generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. It should not the used in the clustered environment.
identity
It supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
Sequence
The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int
hilo The hilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with connections enlisted with JTA or with a user-supplied connection.
Seqhilo
The seqhilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
uuid
The uuid generator uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.
guid
It uses a database-generated GUID string on MS SQL Server and MySQL.
Native
It picks identity, sequence or hilo depending upon the capabilities of the underlying database.
assigned
lets the application to assign an identifier to the object before save() is called. This is the default strategy if no element is specified.
select retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
foreign
uses the identifier of another associated object. Usually used in conjunction with a primary key association.

No comments:

Post a Comment