A collection of Hibernate tutorials

Home arrow Java Tutorials arrow Getting Started arrow A collection of Hibernate tutorials
A collection of Hibernate tutorials Print E-mail
Contributed by Howell   
Thursday, 15 June 2006
 Abstract  (Hibernate tutorials)

Object-relational mapping (O/R mapping) is a common requirement of many software development projects. The activities involved in persisting data are tedious and error-prone. If we also take into consideration the inevitable change of requirements, we're in serious trouble: the data storage structure must be kept in sync with the source code. Add the portability issue, and things are becoming very, very complicated.

Hibernate will help us to painlessly store the data in permanent storage without too much hassle about choosing the kind of storage, installation, or configuration. Hibernate allows us to store any kind of objects; therefore, our application does not need to know that its data will be persisted using Hibernate. Of course, everything mentioned here can be applied in the opposite direction: fetching already prepared objects from a storage is now nearly trivial. Updating and deleting data is also available.

Other Java/Database Integration Solutions (Hibernate tutorials)

Hibernate represents merely one approach to object/relational mapping. Other programming languages and platforms offer a variety of other options, some similar to Hibernate, others radically different. Focusing on the Java world, here are a few of the popular alternatives.

Enterprise JavaBeans (EJB) 2.X

At the core of the J2EE specification, EJB 2.X attempts to provide a much broader set of features and functionality than Hibernate, at the price of a less robust core persistence model. The portion of J2EE/EJB 2.X most comparable to Hibernate is called container-managed entity persistence (CMP). Typically, CMP describes a system in which the J2EE server manages a single-entity bean class per table with individual-entity bean objects representing individual records. Conceptually, entity beans managed by the J2EE server include sophisticated functionality, such as complex transaction management, distributed caching, and much more. The alternative is bean-managed persistence, in which developer code is used to handle interaction with a database.

While EJB 2.X is suitable for certain large-scale enterprise applications, some developers find it difficult to work with. Most EJB 2.X systems involve a considerable amount of code generation and the need to master a complex suite of terminology. In particular, the implementation of the container-managed persistence layer and other components varies greatly between different application servers, making migration between projects that rely on container-managed persistence difficult. A popular use of Hibernate is as a persistence layer while working within the larger EJB 2.X environmentin effect, Hibernate serves as a replacement for container-managed persistence while leveraging the other portions of the EJB 2.X specification. This allows you to take advantage of many of the powerful features of your J2EE server without worrying about the portability of your object/relational layer.

Enterprise JavaBeans (EJB) 3.0

With an early draft specification first released in mid-2004, EJB 3.0 promises a persistence mechanism very similar to Hibernate. This is not surprising; the key developer of Hibernate was heavily involved in crafting the EJB 3.0 specification. Throughout this text, areas of similarity between EJB 3.0 and Hibernate will be highlighted, and additional comparison is provided in Chapter 13.

Java Data Objects (JDO)

Java Data Objects, or JDO, differ more from Hibernate in terms of implementation than from the interface offered to developers. Specifically, JDO normally requires compile-time pre-processing of the Java byte-code in order to add persistence capability, whereas Hibernate performs similar tasks at runtime when the application is first started. There are also differences in the APIs used to query from the persistence source. In particular, JDO attempts to solve a larger issue of persistence, targeting data stores other than relational databases. The attempt to target a broader range of storage mechanisms leads to a different focus than the relational database-focused Hibernate, with a different set of query capabilities. It's unclear whether the goal of targeting nonrelational database persistence engines is worth the tradeoffs.

As of this writing, there has been some discussion of future versions of Hibernate and JDO moving closer together. Hibernate already supports three different interfaces (ODMG, HQL, and Criteria). It's entirely possible that future Hibernate releases may support a JDO style interface as well.

As of this writing, JDO implementations include:

TJDO, or TriActive JDO, is an open-source implementation of the JDO specification. It is explicitly designed to operate as Java persistence layer. In other words, the developer is expected to focus on Java development, with the database schema generated and managed by TJDO. It is not appropriate for situations with an existing schema. For more information, see http://tjdo.sourceforge.net/.

JPOX is another open-source JDO implementation. Unlike TJDO, more emphasis is placed on both schema-centric and Java-centric development. For more information, see http://www.jpox.org/.

JCredo provides a commercial implementation of JDO. A free edition is available for download from the Web site at http://www.jcredo.com/.

Apache OJB, available at http://db.apache.org/ojb/, provides both an ODMB and a JDO interface.

 

HIBERNATE - Features of Hibernate (Hibernate tutorials)

  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 

  Object-oriented query language 

    Powerful object-oriented query language
    Full support for polymorphic queries
    New Criteria queries
    Native SQL queries

  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 

  Automatic primary key generation 

    Multiple synthetic key generation strategies 
    Support for application assigned identifiers 
    Support for composite keys 

  Object/Relational mapping definition  

    XML mapping documents
    Human-readable format
    XDoclet support

  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

  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

  J2EE integration 

    JMX support 
    Integration with J2EE architecture (optional) 
    New JCA support 
 

 How to learn Hibernate for Java (Hibernate tutorials)

 Day 1: Download and Install Hibernate

Download Hibernate 3.1 and extract the archive
place your JDBC driver jar file in the lib directory
edit etc/hibernate.properties, specifying connection settings for your database (Hibernate will create a schema for the demo automatically)
from a command prompt in the install dir
if you have Ant installed (and copied antlr.jar and junit.jar to ANT_HOME/lib), type ant eg
if not, type build eg under Windows
the example should run successfully
browse the sourcecode of the example in the eg directory
edit etc/hibernate.properties again, setting hibernate.show_sql=true
run the example again, and look at the SQL statements Hibernate generates
edit etc/log4j.properties, setting the category org.hibernate to debug and run the example a third time (you can use the Hibernate log to help during troubleshooting)


Day 2: Read the Documentation


Read the tutorial in the reference documentation.
Read the rest of the reference documentation.
Read the FAQ.


Day 3: Start Coding!

Many examples you find on the Net are still Hibernate 1.x or 2.x - read the migration guide for a list of differences to Hibernate3.
Many good patterns can be found on the Wiki Community Area, such as Sessions and transactions, Open Session in View, Generic Data Access Objects, ...
Once you are familiar with basic Hibernate, download the CaveatEmptor example application.

 

 

 

 

External Hibernate articles & tutorials

Articles


Title:  Hibernate Tools: Hibernate-Entwickler mit Eclipse GUI und Ant Tasks unterstützen
Author:  Markus Junginger, Java Magazin
Date:  7.2006
Version:  Hibernate Tools 3.1b4
URL:  http://www.javamagazin.de/itr/online_artikel/psecom,id,822,nodeid,11.html
Notes:  Article that gives a basic overview about how to use Hibernate Tools for Eclipse and Ant.


Title:  Struts Recipes: Hibernate and Struts
Author:  George Franciscus
Date:  28.02.2005
Version:  Hibernate 2.1
URL:  http://www-106.ibm.com/developerworks/library/j-hibern/?ca=dnt-515
Notes:  In this excerpt from Struts Recipes, (Manning Publications, December 2004) authors George Franciscus and Danilo Gurovich illustrate how to use Hibernate in a Struts application. They also show how to create a Struts plug-in to improve performance.


Title:  Object-relation mapping without the container
Author:  Richard Hightower
Date:  17.04.2004 
Version:  Hibernate 2.1
URL:  http://www-106.ibm.com/developerworks/library/j-hibern/?ca=dnt-515
Notes:  Developing a transactional persistence layer using Hibernate and Spring.


Title:  Hibernate Your Data
Author:  Davor Cengija 
Date:  14.01.2004 
Version:  Hibernate 2.0.3 
URL:  http://www.onjava.com/pub/a/onjava/2004/01/14/hibernate.html 
Notes:  Hibernate basics in this article on ONJava.com. Note that the alias in class MyClass HQL syntax is deprecated, use MyClass as alias instead.


Title:  A Simple Data Access Layer using Hibernate
Author:  Mario Aquino
Date:  11/2003
Version:  Hibernate 2.0.3, Middlegen R3
URL:  http://www.ociweb.com/jnb/jnbNov2003.html
Notes:  An article about data access layers with Hibernate, code generation and Command patterns.


Title:  Index of Relationships
Author:  Tom Sedge
Date:  ?
Version:  all
URL:  http://www.xylax.net/hibernate
Notes:  Tom Sedge has written a piece explaining various Hibernate association styles. Note that Tom uses "first rank class" to refer to what the Hibernate documentation calls "entity" and "second rank class" to refer to what the Hibernate documentation calls "value".


Title:  Object to Relational Mapping and Relationships with Hibernate
Author:  Mark Eagle
Date:  ?
Version:  Hibernate 2.0RC1
URL:  http://www.meagle.com:8080/hibernate.jsp
Notes:  Mark Eagle wrote an article with simple example code using XDoclet mappings.


Title:  The Fundamentals of Mapping Objects to Relational
Author:  Scott Ambler
Date:  ?
Version:  N/A
URL:  http://www.agiledata.org/essays/mappingObjects.html
Notes:  Scott Ambler's well-known paper is the about best explanation of O/R mapping strategies around. A must read!


Title:  Manual Hibernate
Author:  Hector Suarez Gonzalez
Date:  16.10.2003
Version:  Hibernate 2.0.x
URL:  http://www.javahispano.org/articles.article.action?id=82
Notes:  Hector Suarez Gonzalez wrote an introduction to Hibernate in Spanish.


Title:  Hibernate - En introduktion
Author:  Torben Norling
Date:  14.01.2004
Version:  Hibernate 2.1.x
URL:  http://www.bluefish.se/aquarium/hibernate-intro.html
Notes:  A Hibernate introduction in Swedish.


Title:  Object Relational Tool Comparison
Author:  various
Date:  maintained
Version:  Hibernate 2.0.3
URL:  http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison
Notes:  The Cayenne project maintains a Wiki page comparing features of various Java O/R tools.

Tutorials


Title:  The Road to Hibernate
Author:  Michael Gloegl
Date:  14.03.2005
Version:  Hibernate 3-rc1
URL:  http://www.gloegl.de/5.html
Notes:  This page contains several Hibernate tutorials by Michael Gloegl, using Ant, Tomcat and WebWork.


Title:  Hibernate / Middlegen - Inheritance and Many To Many
Author:  Tyler Pitchford 
Date:  16.05.2004
Version:  Hibernate 2.1.x
URL:  http://www.warfrog.com/hibernatetutorial2/
Notes:  This tutorial covers how to modify the hbm.xml files generated by Middlegen to correctly produce Inheritance and Many to Many relationships when processed by hbm2java. 


Title:  Hibernate / Spring / Middlegen / XDoclet Tutorial
Author:  Tyler Pitchford 
Date:  30.04.2004
Version:  Hibernate 2.1.x, Middlegen R4
URL:  http://www.warfrog.com/hibernatetutorial/
Notes:  This tutorial is built to help Java developers get Hibernate, Spring, Middlegen and XDoclet up an running in a timely fashion.


Title:  Introduction to Hibernate
Author:  Nick Heudecker
Date:  12/2003
Version:  Hibernate 2.1RC1
URL:  http://www.systemmobile.com/articles/hibernate_intro.php
Notes:  Nick Heudecker wrote an introductional article about Hibernate, discussing basic mappings, object storage and retrieval with HQL.


Title:  Using Hibernate to Persist Your Java Objects to IBM DB2 Universal Database
Author:  Javid Jamae & Kulvir Singh Bhogal
Date:  6/2003
Version:  Hibernate 1.2.x
URL:  http://www7b.software.ibm.com/dmdd/library/techarticle/0306bhogal/0306bhogal.html
Notes:  Javid Jamae and Kulvir Singh Bhogal published an article about Hibernate with DB2 and Websphere on IBM developerWorks. The forum link points to the old one: please use http://forum.hibernate.org


Title:  A Hitchhiker's Guide to Hibernate
Author:  Glen Smith
Date:  ?
Version:  Hibernate 1.2
URL:  http://files.blog-city.com/files/aa/1018/b/HibernateKickstart.html http://freeroller.net/page/aitor/hib
Notes:  Glen Smith wrote a short tutorial-style introduction to Hibernate. There is also a Spanish translation (second link).


Title:  Cocoon HowTos
Author:  Hugo Burm
Date:  11.11.2003
Version:  Hibernate 2.0.x
URL:  http://wiki.cocoondev.org/Wiki.jsp?page=HowTos
Notes:  Hugo Burm wrote a series of three Hibernate + Cocoon "howtos" for the Cocoon wiki.


Title:  Eclipse + Resin + WebWork + Hibernate = Sah-WEET!
Author:  Paul Snively
Date:  17.11.2002
Version:  Hibernate 1.2b2
URL:  http://radio.weblogs.com/0100136/stories/2002/11/15/eclipseResinWebworkHiberna.html


Title:  How to setup a ejb remote application using JBoss and Hibernate
Author:  Nemesis IT
Date:  ?
Version:  ?
URL:  http://nemesisit.rdsnet.ro/opendocs/simplearch/simplejboss.html
Notes:  Two instructionals covering JBoss + Hibernate / Tapestry + Hibernate on Nemesis IT site

Books


 Hibernate in Action
Book Description

Hibernate practically exploded on the Java scene. Why is this open-source tool so popular? Because it automates a tedious task: persisting your Java objects to a relational database. The inevitable mismatch between your object-oriented code and the relational database requires you to write code

 

 


  home              contact us

 

©2006-2009 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground