Hibernate

Hibernate is a high-performance Object/Relational persistence and query service open source framework for Java.

Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve the developer from 95% of common data persistence related programming tasks.

Hibernate sits between traditional Java objects and database server to handle all the work in persisting those objects based on the appropriate O/R mechanisms and patterns.

Hibernate Position
Hibernate Application Architecture.

Configuration Object:

The Configuration object is the first Hibernate object you create in any Hibernate application and usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate. The Configuration object provides two keys components:

    • Database Connection: This is handled through one or more configuration files supported by Hibernate. These files arehibernate.properties and hibernate.cfg.xml.
    • Class Mapping Setup

This component creates the connection between the Java classes and database tables..

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 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.

Session Object:

A 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.

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 not to use this interface, instead managing transactions in their own application code.

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.

Criteria Object:

Criteria object are used to create and execute object oriented criteria queries to retrieve objects.

Hibernate Properties:

Following is the list of important properties you would require to configure for a databases in a standalone situation:

S.N. Properties and Description
1 hibernate.dialect
This property makes Hibernate generate the appropriate SQL for the chosen database.
2 hibernate.connection.driver_classThe JDBC driver class.
3 hibernate.connection.urlThe JDBC URL to the database instance.
4 hibernate.connection.usernameThe database username.
5 hibernate.connection.passwordThe database password.
6 hibernate.connection.pool_sizeLimits the number of connections waiting in the Hibernate database connection pool.
7 hibernate.connection.autocommitAllows autocommit mode to be used for the JDBC connection.

If you are using a database along with an application server and JNDI then you would have to configure the following properties:

S.N. Properties and Description
1 hibernate.connection.datasourceThe JNDI name defined in the application server context you are using for the application.
2 hibernate.jndi.classThe InitialContext class for JNDI.
3 hibernate.jndi.<JNDIpropertyname>Passes any JNDI property you like to the JNDI InitialContext.
4 hibernate.jndi.urlProvides the URL for JNDI.
5 hibernate.connection.usernameThe database username.
6 hibernate.connection.passwordThe database password.

Website

References

  • Hibernate Examples

    Hibernate Examples – Learn Hibernate 3.x starting from environment setup, Object Relational Mapping (ORM), Query Language, Native SQL, Caching, Interceptors, Persistent Classes, Persistent Objects, Collections, Associations and components, and Hibernate tools.

    Source: www.tutorialspoint.com/hibernate/hibernate_examples.htm