Tuesday 1 January 2013

hibernate



 Business Object
A layer between your application code and the DAOs
○ Only allow business objects to hit the DAOs
○ Business objects and app code use persistent objects
Provide a consistent place to use AOP
○ try/catch/finally for transactions
Risk duplicating DAO code
○ Sometimes business object methods just delegate

DAOs
Insulate your code from Hibernate specifics
CRUD operations
○ I use Java 5 generics heavily
○ You can also use Spring for DAO support
Locating persistent objects
○ Use Hibernate queries for efficiency
○ From there, walk the graph using normal Java
collections semantics
Detached objects can be challenging

What is Hibernate?

Hibernate is a high-performance Object/Relational persistence and query service (Object/Relational Mapping (ORM) solution for JAVA).

○ Open source – GNU Lesser General Public License (LGPL)
○ http://www.hibernate.org/ (JBoss Group)


Lets you avoid SQL
Works with (almost) all relational DBs
○ DB2/NT, FrontBase, HSQLDB, Informix Dynamic Server, Ingres, Interbase,
Mckoi, MySQL, Oracle, Pointbase, PostgreSQL, Progress,
SAP DB, Microsoft SQL Server, Sybase SQL Server, etc...
○ Or you can extend one of the Dialect classes

Has a huge feature list
○ Go to their web site – we don't have time

Maps JavaBeans (POJOs) to tables
○ XML defines the mappings
○ Very few bean requirements

SQL is generated at app startup time
○ As opposed to bytecode manipulation
○ New database? No problem! Change a few props


 
Following is a very high level view of the Hibernate Application Architecture.

Following is a detailed view of the Hibernate Application Architecture with few important core classes.
 


 1) Configuration Object
 Create the Hibernate Configuration objects for Hibernate application.
  • Database connection: This is handled through one or more configuration files supported by Hibernate. hibernate.properties and hibernate.cfg.xml
  • Class Mapping Setup : This component creates the connection between the Java classes and database tables.
2) SessionFactory Object
 Configuration object is used to create a SessionFactory object which inturn configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated. The SessionFactory is a thread safe object and used by all the threads of an application.
The SessionFactory is is heavyweight object so usually it is created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file. So if you are using multiple databases then you would have to create multiple SessionFactory objects.


3) Session Object
Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.
The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed.


4) Transaction Object
A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionality. Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).
This is an optional object and Hibernate applications may choose


5) Query Object
Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.

6) Criteria Object:
Criteria object are used to create and execute object oriented criteria queries to retrieve objects.

 Hibernate Environment Setup
1. Download & install the latest version of Hibernate from http://www.hibernate.org/downloads
2. copy all the Hibernate library files from /lib into your CLASSPATH, and change your classpath variable to include all the JARs.
3. Finally copy hibernate3.jar file into your CLASSPATH. This file lies in the root directoru of the installation and is the primary JAR that Hibernate needs to do its work.

Hibernate Prerequisites:
Following is the list of the packages/libraries required by Hibernate and you should install them before starting with Hibernate. To install these packages you would have to copy library files from /lib into your CLASSPATH, and change your CLASSPATH variable accordingly.

1 dom4j - XML parsing www.dom4j.org/
2 Xalan - XSLT Processor http://xml.apache.org/xalan-j/
3 Xerces - The Xerces Java Parser http://xml.apache.org/xerces-j/
4 cglib - Appropriate changes to Java classes at runtime http://cglib.sourceforge.net/
5 log4j - Logging Faremwork http://logging.apache.org/log4j
6 Commons - Logging, Email etc. http://jakarta.apache.org/commons
7 SLF4J - Logging Facade for Java http://www.slf4j.org

Compilation and Execution:
Here are the steps to compile and run the above mentioned application. Make sure you have set PATH and CLASSPATH appropriately before proceeding for the compilation and execution.
1. Create hibernate.cfg.xml configuration file as explained in configuration chapter.
2. Create Employee.hbm.xml mapping file as shown above.
3. Create Employee.java source file as shown above and compile it.
4. Create ManageEmployee.java source file as shown above and compile it.
5. Execute ManageEmployee binary to run the program.


Persistent Objects
POJOs - Plain Old Java Objects
○ No base class or interface requirement
Must have public no-arg constructor
 Should have getter/setters
○ Or Hibernate can access fields directly
○ Hibernate uses JavaBeans property names
For mappings as well as in queries
Serializable is recommended
XDoclet tags are useful but not required

Gotchas
Must always have an open session
○ Seemingly innocent code like fetching a lazy
collection bombs
○ Swing – open a session on event thread
○ Servlets – use a filter or Spring
Hibernate tasks do not abort Ant
○ No failonerror attribute
○ Watch the build output carefully
Test associations and collections heavily

No comments:

Post a Comment