Hibernate

Hibernate is an open source object/relational mapping tool for Java. Hibernate lets you develop persistent classes following common Java idiom – including association, inheritance, polymorphism, composition and the Java collections framework.

Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC.

Hibernates goal is to relieve the developer from 95 percent of common data persistence related programming tasks. Some of the main features of hibernate are listed below and we have tried to explain some of them are…

1. Transparent persistence without byte code processing
* Transparent persistence
* JavaBeans style properties are persisted
* No build-time source or byte code generation / processing
* Support for extensive subset of Java collections API
* Collection instance management
* Extensible type system
* Constraint transparency
* Automatic Dirty Checking
* Detached object support
2. Object-oriented query language
* Powerful object-oriented query language
* Full support for polymorphic queries
* New Criteria queries
* Native SQL queries
3. Object / Relational mappings
* Three different O/R mapping strategies
* Multiple-objects to single-row mapping
* Polymorphic associations
* Bidirectional associations
* Association filtering
* Collections of basic types
* Indexed collections
* Composite Collection Elements
* Lifecycle objects
4. Automatic primary key generation
* Multiple synthetic key generation strategies
* Support for application assigned identifiers
* Support for composite keys
5. Object/Relational mapping definition
* XML mapping documents
* Human-readable format
* XDoclet support
6. HDLCA (Hibernate Dual-Layer Cache Architecture)
* Thread safeness
* Non-blocking data access
* Session level cache
* Optional second-level cache
* Optional query cache
* Works well with others
7. High performance
* Lazy initialization
* Outer join fetching
* Batch fetching
* Support for optimistic locking with versioning/timestamping
* Highly scalable architecture
* High performance
* No “special” database tables
* SQL generated at system initialization time
* (Optional) Internal connection pooling and PreparedStatement caching
8. J2EE integration
* JMX support
* Integration with J2EE architecture (optional)
* New JCA support

Hibernate makes use of persistent objects commonly called as POJO (Plain Old Java Object) along with XML mapping documents for persisting objects to the database layer. The term POJO refers to a normal Java objects that does not serve any other special role or implement any special interfaces of any of the Java frameworks (EJB, JDBC, DAO, JDO, etc…).

Rather than utilize byte code processing or code generation, Hibernate uses runtime reflection to determine the persistent properties of a class. The objects to be persisted are defined in a mapping document, which serves to describe the persistent fields and associations, as well as any subclasses or proxies of the persistent object. The mapping documents are compiled at application startup time and provide the framework with necessary information for a class. Additionally, they are used in support operations, such as generating the database schema or creating stub Java source files.

Typical Hibernate code

SessionFactory sessionFactory = new Configuration()
.configure().buildSessionFactory();
Session session = sessionFactory.openSession();

Transaction tx = session.beginTransaction();

Customer cust= new Customer();
cust.setName(“New Cust”);
cust.setAddress(“New Cust address”);
cust.setEmailId(“cust@techcyclopedia.com”);

session.save(newCustomer);

tx.commit();

session.close();

First step is hibernate application is to retrieve Hibernate Session; Hibernate Session is the main runtime interface between a Java application and Hibernate. SessionFactory allows applications to create hibernate session by reading hibernate configurations file hibernate.cfg.xml.

After specifying transaction boundaries, application can make use of persistent java objects and use session for persisting to the datab

Hibernate is an open source object/relational mapping tool for Java. Hibernate lets you develop persistent classes following common Java idiom – including association, inheritance, polymorphism, composition and the Java collections framework. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but…

Leave a Reply

Your email address will not be published. Required fields are marked *