Hibernate Tutorial

•        Define persistence and object persistence.
•        Understand some of the issues in persisting objects in relational databases.
•        Look at Object Relational Mapping and persistent frameworks for dealing with object relational
•        See the benefits of a persistence framework like Hibernate in a Java application.
•        Examine the origins of Hibernate.  
•        See a simple Hibernate example application.
•        Begin to explore the Hibernate classes and API.
•        Learn how to download and install/setup Hibernate.
•        Explore Hibernate configuration.
•        Start to explore and understand Hibernate mappings.
•        Define a POJO and JavaBean.
•        Explore how Hibernate manages the persistence of POJOs/JavaBeans.
•        Specifically, explore Hibernate basic class mapping.
•        See how properties are mapped to database columns.
•        Understand how identifiers are linked to database primary keys.
•        Learn about the various types of primary keys and how they map to different class identifiers.
•        Explore the persistence lifecycle.
•        Understand the Transient, Persistent and Detached states in the lifecycle.
•        See the methods that transition objects to the various states in the lifecycle.
•        Revisit object identifiers and their importance for managing object persistence and defining state.
•        Learn about Hibernate’s single Persistent object instance guarantee.
•        Explore the concept of Domain Model.
•        Understand the different types of associations or relationships among persistent objects in a domain
•        Define Entity versus Value type in Hibernate.
•        See how a Component type is a kind of Value type.
•        Learn how to create and map a Component type in Hibernate.
•        Learn the purpose of Component types in Hibernate.

Chapter Overview
Hibernate has been described as the “open source darling ” that is gaining in popularity to solve Java object
persistence in many applications.  What is persistence or object persistence?  Why is persisting objects so
difficult and why would a technology like Hibernate help?  In this chapter, these questions are addressed
along with the general topic of object relational mapping.  Whole companies have been built around solving
the object relational mapping problem.  Through this chapter, you will see the fertile field that has led to
Hibernate’s ascension as a prominent tool to tackle this difficult problem.
•        What is persistence?
•        The capability of a computer program to store data in “non-volatile” storage.
•        Allowing the data to exist even when the program is no longer running.
•        Storage options include a file system, relational database, hierarchical database, object database, etc…
•        In Java, most persistence is accomplished by storing data in a relational database management system
•        You use SQL to get data in and out of the relational database.
•        Java Database Connectivity (JDBC) is Java’s API to connect to the RDBMS and send in SQL
Object Persistence                                                                
•        What is Object Persistence?
•        Dealing with persistence in object oriented programs like Java.
•        Determining how objects and their relationships are persisted in a relational database.
•        Object persistence is about mapping object state.
•        Determining how an object’s state (data stored in member variables of an object) is stored in database
table columns.

•        It is also about dealing with the fact that object state types may not align with relational database types.
Some Java Data Types vs. SQL Data Types
Java Data Type        SQL Data Type
String        VARCHAR
String        CHARACTER
String        LONGVARCHAR
BigDecimal        NUMERIC
BigDecimal        DECIMAL
Boolean, boolean        BIT
Integer, byte        TINYINT
Integer, short        SMALLINT
Integer, long        BIGINT
Integer, int        INTEGER
Float, float        REAL
Double, double        FLOAT
Double, double        DOUBLE PRECISION
•         Object persistence is also about mapping object relationships.
•        Determining how associations between objects are stored as relational database keys or in relational
database intersection tables.

Object/Relational Paradigms                                                
•        Through languages like Smalltalk, C++ and Java, software developers have been sold on the benefits of
object oriented programming.
•        OOP is based on software engineering principles.
•        Objects represent both behavior and state (or data).
•        Objects model or mimic the business domain (the real world).
•        Objects are easily traversed through relationship graphs (inheritance, associations).
•        Object identity is somewhat flexible.
•        However, objects are not good for dealing with large amounts of information all at once.
•        Relational databases are proven and entrenched data persistence technology.
•        Databases are based on mathematical principles.
•        Concerned with data and not behavior.
•        Used to store and retrieve large amounts of data.
•        However, they are not as good for business modeling.
•        They rely on table joins to associate data.
•        Unique identity in databases is critical.

•        Getting state (data) and associations from objects into relational table structure and vice versa requires a
lot of tedious programming.
•        Various studies have estimated that between 30-70%  of application development is spent on this

•        This can create problems in scalability, maintenance and flexibility
•        Requires the object programmer and relational database administrator to develop a common view or
•        This is not always easy to achieve
•        It usually results in a less than optimal model for one or both.
•        Incongruence between object and relational structures even has names.
•        Impedance mismatch
•        Object/Relational paradigm mismatch
•        Object-data divide (Scott Ambler)
Hibernate Tutorial
Table of Contents
Training Resources
Copyright (c) 2008-2013.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.