SlideShare a Scribd company logo
Java Persistence with
Hibernate
It ain’t about winter sleeping
Outline
 What is Hibernate?
 Why Hibernate?
 What are the alternatives to Hibernate?
 Object relational mapping issues
 Simple Hibernate example
 Hibernate Power
 Advanced association mapping
 Advanced inheritance mapping
 Flexibility and extensibility
 Hibernate design consideration (tips and
tricks)
What is hibernate?
 Hibernate is an object-relational mapping
framework (established in 2001)
 In English, that means it provides tools to
get java objects in and out of DB
 It also helps to get their relationships to other
objects in and out of DB
 It does so without a lot of intrusion
 You don’t have to know a lot of hibernate API
 You don’t have to know or use a lot of SQL
Why use hibernate?
 Too much developer time is spent
getting data in and out of the
database and objects (Java Objects)
 Take a guess !
That’s
30% – 70%
Of your developer’s time
Why Hibernate …
 Hibernate’s stated goal “is to relieve the
developer from 95 percent of common data
persistence related programming tasks”
 Conservative numbers – 95% of 30% >= 25%
increase in productivity
 Hibernate can also help your application be
more platform independent/portable
 Database access configuration and SQL are
moved to configuration files
 Hibernate can help with performance
 It generates efficient queries consistently
Alternatives
 Hibernate is certainly not the only ORM framework
 There are lot more
 Java Persistence API – From Sun (mature now, but was
only a specification when hibernate is used in industry)
 Java Data Objects – Requires additional compilation
steps that alters .class files
 EJB Container Managed Persistence (CMP) – Pretty
complex technology but was the sole provider of ORB
before the evolution of other ORM framework and got
lot of container specific features, cannot be used in
standalone
 iBatis – Requires lot of SQL skills
 TopLink – Is a commercial product now owned by Oracle
 Castor –Open Source ORM but not as popular as
hibernate
Why so many ORMs?
 Mapping objects to relational database is hard
 The two systems satisfy different need
 There are a number of obstacles to overcome based
on the differences
 Identity
 Granularity
 Associations
 Navigation
 Inheritance
 Data type mismatches
 All above needs to be overcome for an ORM to be
successful
Identity
 A row is uniquely identified from all other
rows by its identity
 An object’s identity doesn’t always translate
well to the concept of primary key
 In Java, what is an object’s identity? Its
data or its place in memory?
 Consider the following: are two objects equal or
identical?
accountA.equals(accountB)
accountA == accountB
Granularity
 Objects and tables are modeled with
different levels of granularity
 Table structures are often “de-
normalized” in order to support better
performance
 Table rows end up mapping to multiple
objects
 Objects tend to be more fine-grained
Associations
 Associations in Java are either unidirectional or
bidirectional
 Based on attributes and accessors
 Cardinality is represented by complex types (List,
Set)
 Object identification can impact certain associations
(Set)
 Relating data in database tables is based on joins
 There is no concept of directionality
 Many to Many relation require the addition of a join
table
 Identity, especially foreign key identity, greatly
impacts associations.
Association example
Navigation and Association
Traversal
 Navigating Java Objects to get property information
often required traversal of the object graph
 This can be quite expensive if the objects are
distributed or need to be created before traversed
 Consider the below
 Navigating in database is handled, potentially, by a
single join
 select a.street, a.city, a.state, a.zip from address
a, order o, account t, customer c where
o.id=t.order_id and t.customer_id=c.id and
c.address_id=a.id
Inheritance
 Inheritance and Polymorphism are
important concepts in OO programming
languages like Java
 Add power and flexibility to our programming
environments
 Databases have no equivalent concepts to
inheritance
 Mapping inheritance trees in Java to the
database creates an opportunity for
creativeness
Inheritance example
Datatypes
 Even simple types are not easily
mapped between Java and the major
relational databases
 Do you remember some case?
Datatypes Mapping example
Hibernate to the rescue
 Hibernate handles all of these issues
 In fact, Hibernate provides several options
in most cases to allow you to handle them
in a variety of ways
 Why the options?
 The database may have come before the
application (lay on top of legacy systems)
 The application may drive the development of
the database (green field applications)
 The database and application must be integrated
(meet-in-the-middle approach)
A Simple Example of Hibernate
Creating and Saving
Retrieving
Updating
Configuring Hibernate
 In order to get the previous examples work, two types
of configuration files must also be provided
 A hibernate cfg.xml file defines all the database
connection information
 This can be done in standard property file or xml
format
 The configuration supports both managed (app
server with data source) or non-managed
environments
 Allowing of easy to setup and use dev, test, prod
environments
 A classname.hbm.xml maps class properties to the
database table/columns
 Traditionally one configuration per class
 Stored with the .class files
Configuration files
Hibernate Architecture and API
Configuration
 As seen in the example code, a Configuration object is
the first Hibernate object you use
 The Configuration object is usually created once
during application initialization
 The Configuration object reads and establishes the
properties Hibernate uses to get connected
 By default, the configuration loads configuration
property information and mapping files from the
classpath
 The configuration object can be told explicitly where
to find files
 The Configuration object is used to create a
SessionFactory and then typically discarded.
SessionFactory
 The SessionFactory is created from a Configuration
object
 SessionFactory sf = cfg.buildSessionFactory();
 The SessionFactory is an expensive object to create
 It too is usually created during application startup
 It should be created once and kept for later use
 The SessionFactory object is used by all the threads
of an application
 It is a thread safe object
 One SessionFactory object is created per database
 The SessionFactory is used to create Session objects
Session
 The Session object is created from the SessionFactory
object
 Session session = sf.openSession();
 A Session object is lightweight and inexpensive to create
 Provides the main interface to accomplish work with database
 Does the work of getting a physical connection to database
 Not thread safe
 Should not be kept open for a long time
 Applications create and destroy these objects as needed. They
are created to complete a single unit of work
 When modifications are made to the database, session
objects are used to create a transaction object
Transaction
 Transaction objects are obtained from a
session object when a modification to the
database is needed
 Transaction trx = session.beginTransaction();
 The Transaction object provides abstraction
for the underlying implementation
 Hibernate with different transaction
implementation is available (JTA, JDBC, etc)
 It is optional, allowing developers to use their
own transactional infrastructure
 Transaction object should be kept open for
as short of time as possible
That’s the API Basics
 Configuration, SessionFactory, Session, Transaction (along
with your own persistent classes) gets you started
 In fact, most of the hibernate “coding” is spent in the two
configuration files
 hibernate.cfg.xml or hibernate.properties
 Classname.hbm.xml (one per persistent class)
 The toughest part is getting your persistent mapping set up
 Understanding and picking the best option for properties
 Understanding and picking the best option for associations
 Understanding and picking the best option for your inheritance
tree
Object Lifecycle
 Persistent object (like the Customer object) are
synchronized with database
 That is, the object’s state and the affected database
rows are kept the same whenever either changes
 Domain objects are in one of three states
 Transient
 Persistent
 Detached
 An object’s state determine if it kept synchronized
with the database
 The presence of a Session and certain method calls
move an object between states
Object State
Persistent to Detached
 We have already seen an example of moving a
new transient object to a persistent state
 Here is an example of an object in detached
state
Detached to Persistent
Back to Transient
Associations
 Hibernate provides rich set of alternatives for
mapping object associations in the database
 Various multiplicities of relationships
 Many to One
 One to Many
 Many to Many
 One to One
 Providing for Java’s understanding of directionality
 Unidirectional
 Bidirectional
 Providing for fine grained control of “transitive
persistence”
Transitive Persistence
 Controls when/how associated objects
react to changes in the other
 For example, should associated address
rows be deleted when a customer object
is removed (cascade delete)
 When an new order is saved should
associated new line item objects also
saved (cascade save)
Association Mapping
 Association mapping is
primarily handled in
the class mapping files
 The class must provide
the standard property
and getter/setter
methods
 For example, an Order
that has an association
to a set of Order Items
would need definitions
similar to those at
right.
Example one-to-many mapping
Bidirectional
 To make the association bidirection, OrderItem
would also need appropriate getters/setters
and this mapping
Other types of Associations
 Hibernate provides for the other types of
multiplicity
 One to One
 Many to Many
 It also supports some unique mappings
 Component mapping (Customer/Address where
both objects are in the same row in DB)
 Collection of Components
 Collection of “value types” like date objects,
String, etc
 Polymorphic associations (association to a
payment might actually be a check or credit card
payment object)
Inheritance
 Hibernate also handles inheritance
associations
 In fact, it supports three strategies
with regard to inheritance mapping
 Table per concrete class
 Table per subclass
 Table per class hierarchy
 Let’s look at an example inheritance
tree and examine the options
Table per concrete class
Table per Subclass
Table per Class Hierarchy
Inheritance in the Mapping
Queries
 Hibernate supports several ways of getting
objects recreated from data in the database
 Hibernate also has non-object query capabilities
for things like reports
 Single object retrieval has already been
demonstrated
 session.get(Customer.class, id)
 Standard SQL can be used when absolutely
necessary
 The query capabilities are too numerous to
show here, but we can give you a few
examples of the options
HQL
 Hibernate Query Language (HQL) is an
object oriented query language for
performing queries
 ANSI SQL based
 Provides parameter binding like JDBC
 Examples
Query q = session.createQuery(“from Customer”);
List list = q.list(); // get all customers in a list
Customer tom = (Customer) session.createQuery(
“from Customer c where c.id=1”).uniqueResult(); //
single customer
Criteria Queries
 As an alternative to HQL, Hibernate offers criteria
queries
 More object oriented in approach
 Queries can be partially checked in compile time
versus runtime
 Examples
List list = session.createCriteria( Customer.class ).add(
Restrictions.eq(“gender”, “F”)).list(); // retrieve all
female customers
List list2 = session.createCriteria( Customer.class
).add(Restrictions.like(“name”, “D%”)).addOrder(Order.
asc(“dateOfBirth”)).list(); // all customers ordered by
dateOfBirth whose name starts with D
Fetching Strategies
 Along with fetching objects there is a problem when
objects are related to other objects
 When you fetch Customer, should you also fetch
associated, Address, Order, OrderItem etc. objects?
 If not, what happens when you do something like the
following?
Customer date = (Customer) session.get(Customer.class, new Long(3));
dave.getAddress();
 Object graphs can be quite large and complex. How
much and when should objects be retrieved?
Proxy Objects
 Hibernate provides for specifying both lazy and
eager fetching
 When lazy fetching is specified, it provides objects as
stand-ins for associated objects
 Proxy objects get replaced by the framework when a
request hits a proxy.
Flexibility and Extendibility
 Hibernate can be modified or extended
 If you don’t like the way Hibernate handles a part of your
persistence or you need capabilities it doesn’t offer, you can
modify its functionality
 Of course, it is open source, but there are also a number of
extension points
 Extension points include
 Dialects for different databases
 Custom Mapping Types
 Identifier Generator
 Cache & CacheProvider
 Transaction & TransactionFactory
 PropertyAccessor
 ProxyFactory
 ConnectionProvider
Good Design
 As with any technology, learning Hibernate
is not hard
 Using it effectively takes time and practice
 There are a number of design patterns and tips
 Some are actually provided with the hibernate
documentation
 Checkout Hibernate Tips/Tricks FAQ
 More background is needed to discuss some
of the patterns and tips/tricks
 Some make good sense no matter what ORM
tool is used
 A few are apparent even with the knowledge
gained today
Layered Design Pattern
 Hibernate provides the means to perform
persistence without code intrusion
 However, this does not negate the need for
appropriate data access layers
 A common pattern applied to Hibernate
applications is the separate layers and
provide appropriate interfacing
 Allows Hibernate or other ORM to be more
easily replaced or extended
 Provides appropriate separation of concerns
Hibernate Utils
Hibernate Tips/Tricks
 Get a SessionFactory early and keep it
 Get Session and Transaction close to the unit of work
and drop them
 Load Lazy by default (for individual objects)
 Use HQL or Criteria with eager fetching when subject
to N + 1 fetches
 Hibernate offers two levels of Cache. Take care when
configuring either
 Bags can provide horrible Collection performance
 If properties are only read but never updated, mark
them appropriately
Resources
 www.hibernate.org
 http://theserverside.com
 Books
 Hibernate in Action (Manning)
 Hibernate a Developer’s Notebook (O’Reilly)
 Java Persistence with Hibernate (Manning)
 Hibernate: A J2EE Developer’s Guide (Addison
Wesley)

More Related Content

PDF
Free Hibernate Tutorial | VirtualNuggets
PPT
Java hibernate orm implementation tool
PPTX
Entity Framework Overview
PPTX
Entity Framework - Entity Data Model (edm)
PPT
ADO.NET Entity Framework
PPTX
Entity Framework - Queries
PPT
Learn HIBERNATE at ASIT
PPTX
Entity Framework Database and Code First
Free Hibernate Tutorial | VirtualNuggets
Java hibernate orm implementation tool
Entity Framework Overview
Entity Framework - Entity Data Model (edm)
ADO.NET Entity Framework
Entity Framework - Queries
Learn HIBERNATE at ASIT
Entity Framework Database and Code First

What's hot (20)

PPTX
Entity Framework - Object Services
PDF
NHibernate (The ORM For .NET Platform)
PDF
Hibernate Interview Questions | Edureka
PPTX
Introducing Domain Driven Design - codemash
PPTX
Entity Framework
PPTX
Introducing Entity Framework 4.0
PPT
What is hibernate?
PPTX
Oracle Sql Developer Data Modeler 3 3 new features
PDF
Tactical DDD (just better OOP?) - PHPBenelux 2017
PPTX
Brownfield Domain Driven Design
PPTX
Applying Domain-Driven Design to craft Rich Domain Models
PPT
Flyweight pattern
PPT
Application Hosting
PPT
Entity framework 4.0
PPT
LINQ to Relational in Visual Studio 2008 SP1
PDF
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...
PDF
Domain Driven Design and NoSQL TLV
PPTX
Building N Tier Applications With Entity Framework Services 2010
PPTX
Building nTier Applications with Entity Framework Services
PDF
Building High Scalability Apps With Terracotta
Entity Framework - Object Services
NHibernate (The ORM For .NET Platform)
Hibernate Interview Questions | Edureka
Introducing Domain Driven Design - codemash
Entity Framework
Introducing Entity Framework 4.0
What is hibernate?
Oracle Sql Developer Data Modeler 3 3 new features
Tactical DDD (just better OOP?) - PHPBenelux 2017
Brownfield Domain Driven Design
Applying Domain-Driven Design to craft Rich Domain Models
Flyweight pattern
Application Hosting
Entity framework 4.0
LINQ to Relational in Visual Studio 2008 SP1
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...
Domain Driven Design and NoSQL TLV
Building N Tier Applications With Entity Framework Services 2010
Building nTier Applications with Entity Framework Services
Building High Scalability Apps With Terracotta
Ad

Similar to Hibernate (20)

PDF
Hibernate 3
PPT
Hibernate
PPTX
Hibernate tutorial
PPT
Introduction to odbms
DOCX
Hibernate3 q&a
PPTX
Hibernate ppt
PPTX
java framwork for HIBERNATE FRAMEWORK.pptx
PPT
Introduction to Hibernate
PDF
Hibernate training at HarshithaTechnologySolutions @ Nizampet
PPTX
Hibernate Training Session1
PPT
7 data management design
PPT
Patni Hibernate
PPT
Hibernate Session 1
PPTX
Hibernate in Action
PPT
Ruby On Rails
DOCX
What is hibernate?
PDF
Hibernate.pdf
DOCX
TY.BSc.IT Java QB U6
PDF
What is hibernate?
PDF
Hibernate Interview Questions and Answers
Hibernate 3
Hibernate
Hibernate tutorial
Introduction to odbms
Hibernate3 q&a
Hibernate ppt
java framwork for HIBERNATE FRAMEWORK.pptx
Introduction to Hibernate
Hibernate training at HarshithaTechnologySolutions @ Nizampet
Hibernate Training Session1
7 data management design
Patni Hibernate
Hibernate Session 1
Hibernate in Action
Ruby On Rails
What is hibernate?
Hibernate.pdf
TY.BSc.IT Java QB U6
What is hibernate?
Hibernate Interview Questions and Answers
Ad

Recently uploaded (20)

PDF
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
PPTX
fundraisepro pitch deck elegant and modern
PDF
natwest.pdf company description and business model
PPTX
FINAL TEST 3C_OCTAVIA RAMADHANI SANTOSO-1.pptx
PPTX
ANICK 6 BIRTHDAY....................................................
PPTX
Sustainable Forest Management ..SFM.pptx
PPTX
Anesthesia and it's stage with mnemonic and images
PPTX
Introduction-to-Food-Packaging-and-packaging -materials.pptx
PPTX
Relationship Management Presentation In Banking.pptx
PDF
Tunisia's Founding Father(s) Pitch-Deck 2022.pdf
PPTX
water for all cao bang - a charity project
PPTX
BIOLOGY TISSUE PPT CLASS 9 PROJECT PUBLIC
PPTX
Tablets And Capsule Preformulation Of Paracetamol
DOCX
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
PPTX
NORMAN_RESEARCH_PRESENTATION.in education
PDF
Nykaa-Strategy-Case-Fixing-Retention-UX-and-D2C-Engagement (1).pdf
PPTX
lesson6-211001025531lesson plan ppt.pptx
PPT
First Aid Training Presentation Slides.ppt
PPTX
Impressionism_PostImpressionism_Presentation.pptx
PPTX
ART-APP-REPORT-FINctrwxsg f fuy L-na.pptx
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
fundraisepro pitch deck elegant and modern
natwest.pdf company description and business model
FINAL TEST 3C_OCTAVIA RAMADHANI SANTOSO-1.pptx
ANICK 6 BIRTHDAY....................................................
Sustainable Forest Management ..SFM.pptx
Anesthesia and it's stage with mnemonic and images
Introduction-to-Food-Packaging-and-packaging -materials.pptx
Relationship Management Presentation In Banking.pptx
Tunisia's Founding Father(s) Pitch-Deck 2022.pdf
water for all cao bang - a charity project
BIOLOGY TISSUE PPT CLASS 9 PROJECT PUBLIC
Tablets And Capsule Preformulation Of Paracetamol
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
NORMAN_RESEARCH_PRESENTATION.in education
Nykaa-Strategy-Case-Fixing-Retention-UX-and-D2C-Engagement (1).pdf
lesson6-211001025531lesson plan ppt.pptx
First Aid Training Presentation Slides.ppt
Impressionism_PostImpressionism_Presentation.pptx
ART-APP-REPORT-FINctrwxsg f fuy L-na.pptx

Hibernate

  • 1. Java Persistence with Hibernate It ain’t about winter sleeping
  • 2. Outline  What is Hibernate?  Why Hibernate?  What are the alternatives to Hibernate?  Object relational mapping issues  Simple Hibernate example  Hibernate Power  Advanced association mapping  Advanced inheritance mapping  Flexibility and extensibility  Hibernate design consideration (tips and tricks)
  • 3. What is hibernate?  Hibernate is an object-relational mapping framework (established in 2001)  In English, that means it provides tools to get java objects in and out of DB  It also helps to get their relationships to other objects in and out of DB  It does so without a lot of intrusion  You don’t have to know a lot of hibernate API  You don’t have to know or use a lot of SQL
  • 4. Why use hibernate?  Too much developer time is spent getting data in and out of the database and objects (Java Objects)  Take a guess ! That’s 30% – 70% Of your developer’s time
  • 5. Why Hibernate …  Hibernate’s stated goal “is to relieve the developer from 95 percent of common data persistence related programming tasks”  Conservative numbers – 95% of 30% >= 25% increase in productivity  Hibernate can also help your application be more platform independent/portable  Database access configuration and SQL are moved to configuration files  Hibernate can help with performance  It generates efficient queries consistently
  • 6. Alternatives  Hibernate is certainly not the only ORM framework  There are lot more  Java Persistence API – From Sun (mature now, but was only a specification when hibernate is used in industry)  Java Data Objects – Requires additional compilation steps that alters .class files  EJB Container Managed Persistence (CMP) – Pretty complex technology but was the sole provider of ORB before the evolution of other ORM framework and got lot of container specific features, cannot be used in standalone  iBatis – Requires lot of SQL skills  TopLink – Is a commercial product now owned by Oracle  Castor –Open Source ORM but not as popular as hibernate
  • 7. Why so many ORMs?  Mapping objects to relational database is hard  The two systems satisfy different need  There are a number of obstacles to overcome based on the differences  Identity  Granularity  Associations  Navigation  Inheritance  Data type mismatches  All above needs to be overcome for an ORM to be successful
  • 8. Identity  A row is uniquely identified from all other rows by its identity  An object’s identity doesn’t always translate well to the concept of primary key  In Java, what is an object’s identity? Its data or its place in memory?  Consider the following: are two objects equal or identical? accountA.equals(accountB) accountA == accountB
  • 9. Granularity  Objects and tables are modeled with different levels of granularity  Table structures are often “de- normalized” in order to support better performance  Table rows end up mapping to multiple objects  Objects tend to be more fine-grained
  • 10. Associations  Associations in Java are either unidirectional or bidirectional  Based on attributes and accessors  Cardinality is represented by complex types (List, Set)  Object identification can impact certain associations (Set)  Relating data in database tables is based on joins  There is no concept of directionality  Many to Many relation require the addition of a join table  Identity, especially foreign key identity, greatly impacts associations.
  • 12. Navigation and Association Traversal  Navigating Java Objects to get property information often required traversal of the object graph  This can be quite expensive if the objects are distributed or need to be created before traversed  Consider the below  Navigating in database is handled, potentially, by a single join  select a.street, a.city, a.state, a.zip from address a, order o, account t, customer c where o.id=t.order_id and t.customer_id=c.id and c.address_id=a.id
  • 13. Inheritance  Inheritance and Polymorphism are important concepts in OO programming languages like Java  Add power and flexibility to our programming environments  Databases have no equivalent concepts to inheritance  Mapping inheritance trees in Java to the database creates an opportunity for creativeness
  • 15. Datatypes  Even simple types are not easily mapped between Java and the major relational databases  Do you remember some case?
  • 17. Hibernate to the rescue  Hibernate handles all of these issues  In fact, Hibernate provides several options in most cases to allow you to handle them in a variety of ways  Why the options?  The database may have come before the application (lay on top of legacy systems)  The application may drive the development of the database (green field applications)  The database and application must be integrated (meet-in-the-middle approach)
  • 18. A Simple Example of Hibernate
  • 22. Configuring Hibernate  In order to get the previous examples work, two types of configuration files must also be provided  A hibernate cfg.xml file defines all the database connection information  This can be done in standard property file or xml format  The configuration supports both managed (app server with data source) or non-managed environments  Allowing of easy to setup and use dev, test, prod environments  A classname.hbm.xml maps class properties to the database table/columns  Traditionally one configuration per class  Stored with the .class files
  • 25. Configuration  As seen in the example code, a Configuration object is the first Hibernate object you use  The Configuration object is usually created once during application initialization  The Configuration object reads and establishes the properties Hibernate uses to get connected  By default, the configuration loads configuration property information and mapping files from the classpath  The configuration object can be told explicitly where to find files  The Configuration object is used to create a SessionFactory and then typically discarded.
  • 26. SessionFactory  The SessionFactory is created from a Configuration object  SessionFactory sf = cfg.buildSessionFactory();  The SessionFactory is an expensive object to create  It too is usually created during application startup  It should be created once and kept for later use  The SessionFactory object is used by all the threads of an application  It is a thread safe object  One SessionFactory object is created per database  The SessionFactory is used to create Session objects
  • 27. Session  The Session object is created from the SessionFactory object  Session session = sf.openSession();  A Session object is lightweight and inexpensive to create  Provides the main interface to accomplish work with database  Does the work of getting a physical connection to database  Not thread safe  Should not be kept open for a long time  Applications create and destroy these objects as needed. They are created to complete a single unit of work  When modifications are made to the database, session objects are used to create a transaction object
  • 28. Transaction  Transaction objects are obtained from a session object when a modification to the database is needed  Transaction trx = session.beginTransaction();  The Transaction object provides abstraction for the underlying implementation  Hibernate with different transaction implementation is available (JTA, JDBC, etc)  It is optional, allowing developers to use their own transactional infrastructure  Transaction object should be kept open for as short of time as possible
  • 29. That’s the API Basics  Configuration, SessionFactory, Session, Transaction (along with your own persistent classes) gets you started  In fact, most of the hibernate “coding” is spent in the two configuration files  hibernate.cfg.xml or hibernate.properties  Classname.hbm.xml (one per persistent class)  The toughest part is getting your persistent mapping set up  Understanding and picking the best option for properties  Understanding and picking the best option for associations  Understanding and picking the best option for your inheritance tree
  • 30. Object Lifecycle  Persistent object (like the Customer object) are synchronized with database  That is, the object’s state and the affected database rows are kept the same whenever either changes  Domain objects are in one of three states  Transient  Persistent  Detached  An object’s state determine if it kept synchronized with the database  The presence of a Session and certain method calls move an object between states
  • 32. Persistent to Detached  We have already seen an example of moving a new transient object to a persistent state  Here is an example of an object in detached state
  • 35. Associations  Hibernate provides rich set of alternatives for mapping object associations in the database  Various multiplicities of relationships  Many to One  One to Many  Many to Many  One to One  Providing for Java’s understanding of directionality  Unidirectional  Bidirectional  Providing for fine grained control of “transitive persistence”
  • 36. Transitive Persistence  Controls when/how associated objects react to changes in the other  For example, should associated address rows be deleted when a customer object is removed (cascade delete)  When an new order is saved should associated new line item objects also saved (cascade save)
  • 37. Association Mapping  Association mapping is primarily handled in the class mapping files  The class must provide the standard property and getter/setter methods  For example, an Order that has an association to a set of Order Items would need definitions similar to those at right.
  • 39. Bidirectional  To make the association bidirection, OrderItem would also need appropriate getters/setters and this mapping
  • 40. Other types of Associations  Hibernate provides for the other types of multiplicity  One to One  Many to Many  It also supports some unique mappings  Component mapping (Customer/Address where both objects are in the same row in DB)  Collection of Components  Collection of “value types” like date objects, String, etc  Polymorphic associations (association to a payment might actually be a check or credit card payment object)
  • 41. Inheritance  Hibernate also handles inheritance associations  In fact, it supports three strategies with regard to inheritance mapping  Table per concrete class  Table per subclass  Table per class hierarchy  Let’s look at an example inheritance tree and examine the options
  • 44. Table per Class Hierarchy
  • 46. Queries  Hibernate supports several ways of getting objects recreated from data in the database  Hibernate also has non-object query capabilities for things like reports  Single object retrieval has already been demonstrated  session.get(Customer.class, id)  Standard SQL can be used when absolutely necessary  The query capabilities are too numerous to show here, but we can give you a few examples of the options
  • 47. HQL  Hibernate Query Language (HQL) is an object oriented query language for performing queries  ANSI SQL based  Provides parameter binding like JDBC  Examples Query q = session.createQuery(“from Customer”); List list = q.list(); // get all customers in a list Customer tom = (Customer) session.createQuery( “from Customer c where c.id=1”).uniqueResult(); // single customer
  • 48. Criteria Queries  As an alternative to HQL, Hibernate offers criteria queries  More object oriented in approach  Queries can be partially checked in compile time versus runtime  Examples List list = session.createCriteria( Customer.class ).add( Restrictions.eq(“gender”, “F”)).list(); // retrieve all female customers List list2 = session.createCriteria( Customer.class ).add(Restrictions.like(“name”, “D%”)).addOrder(Order. asc(“dateOfBirth”)).list(); // all customers ordered by dateOfBirth whose name starts with D
  • 49. Fetching Strategies  Along with fetching objects there is a problem when objects are related to other objects  When you fetch Customer, should you also fetch associated, Address, Order, OrderItem etc. objects?  If not, what happens when you do something like the following? Customer date = (Customer) session.get(Customer.class, new Long(3)); dave.getAddress();  Object graphs can be quite large and complex. How much and when should objects be retrieved?
  • 50. Proxy Objects  Hibernate provides for specifying both lazy and eager fetching  When lazy fetching is specified, it provides objects as stand-ins for associated objects  Proxy objects get replaced by the framework when a request hits a proxy.
  • 51. Flexibility and Extendibility  Hibernate can be modified or extended  If you don’t like the way Hibernate handles a part of your persistence or you need capabilities it doesn’t offer, you can modify its functionality  Of course, it is open source, but there are also a number of extension points  Extension points include  Dialects for different databases  Custom Mapping Types  Identifier Generator  Cache & CacheProvider  Transaction & TransactionFactory  PropertyAccessor  ProxyFactory  ConnectionProvider
  • 52. Good Design  As with any technology, learning Hibernate is not hard  Using it effectively takes time and practice  There are a number of design patterns and tips  Some are actually provided with the hibernate documentation  Checkout Hibernate Tips/Tricks FAQ  More background is needed to discuss some of the patterns and tips/tricks  Some make good sense no matter what ORM tool is used  A few are apparent even with the knowledge gained today
  • 53. Layered Design Pattern  Hibernate provides the means to perform persistence without code intrusion  However, this does not negate the need for appropriate data access layers  A common pattern applied to Hibernate applications is the separate layers and provide appropriate interfacing  Allows Hibernate or other ORM to be more easily replaced or extended  Provides appropriate separation of concerns
  • 55. Hibernate Tips/Tricks  Get a SessionFactory early and keep it  Get Session and Transaction close to the unit of work and drop them  Load Lazy by default (for individual objects)  Use HQL or Criteria with eager fetching when subject to N + 1 fetches  Hibernate offers two levels of Cache. Take care when configuring either  Bags can provide horrible Collection performance  If properties are only read but never updated, mark them appropriately
  • 56. Resources  www.hibernate.org  http://theserverside.com  Books  Hibernate in Action (Manning)  Hibernate a Developer’s Notebook (O’Reilly)  Java Persistence with Hibernate (Manning)  Hibernate: A J2EE Developer’s Guide (Addison Wesley)