Guide to Java Persistence and Hibernate

Sebastian Hennebrueder

Revision History
Revision Mai 2013SH

Table of Contents

About the author, book and versions
The author
The book
Library Versions
I. Introduction
1. Introduction to Hibernate
A first Hibernate example
Creating Java Project and classes
Hibernate configuration
Annotation or XML
Create a session factory
Configuring Log4J
Create database and tables
Create a test client
Using MyEclipse for Hibernate projects
Adding libraries and Hibernate capabilities
Generate Hibernate mappings from existing db
Hibernate basics
What is Hibernate?
Powerful mapping
Powerful query languages
2. Hibernate Concepts - State of Objects
The three states of objects
Lazy initialization, a Hibernate problem
3. Working with Objects
Java Persistence versus Hibernate
Hibernate API
Additional commands
EntityManager API
Additional commands
4. A more complex example – web application
II. Mapping, Queries
5. Basic Mappings
Annotation versus XML
Annotation mapping
Mapping fields
Where to put annotations
XML Mapping
Field mapping
Class mapping
6. Primary key mapping
Natural versus Surrogate Ids
Assigned Id
Generated with Auto Strategy
Other Annotation Strategies
Composite Id
Equals and Hashcode
Other XML Id tags
7. Relation mapping
Selecting between List, Set, Map or array to hold many side
Uni- and Bi-directional relations
1:1 relation
Recursive relation
Typed relation (XML only)
Typed relation (annotation workaround)
8. Components = Composition mapping
Composition versus entity relations
Composed class in one table
Composition as set of many classes
Equals implementation
Composition as list of many classes
Advanced details
Composition 1:n:1
Not included mappings
9. Inheritance
Use Case
Overview on mapping approaches
Single Table
Joined Inheritance
Joined Inheritance with Discriminator
Mixing Single table and Joined
Union Inheritance
XML Includes
Mapped Super Class
10. Lob with Oracle and PostgreSQL
11. Querying data
Useful tools
Beam me into the code
JBoss Tools
Squirrel SQL Client
Select objects, scalars, etc
Simple select
Select with a unique result
Select with join returning multiple objects
Select with join returning one object
Select with join returning distinct results
Selecting a single column (scalar values)
Selecting multiple columns (scalar values)
Selecting objects and scalar values
Selecting selective properties of a class
Simple where condition
Walking through relations
Where condition in a related object
Where condition with parameters
Conditions on collections
Where condition with mapped class
Where condition with mapped class on the multiple side
All, In, Some, Exists, Any elements queries
Criteria Queries
Simple select
Select with a unique result
Select with join returning objects multiple times
Select with join returning distinct objects
Select with a where condition on a related object
Selecting a single column (scalar values, projections)
Simple where condition
Where condition in relation
Where with or condition
Conditions on collections
All, In, Some, Any elements queries
Native SQL
SQL to Entity
SQL Resultset Mapping
Named SQL Queries
JDBC Connection
III. Building applications and Architecture
12. Data Access Objects
Best practices and DAO
Data Access Objects DAO
Weaving the application structure
Paper book order
eBook order
Defining the application layers
DAO and DaoFactory
Creating DAOs with generics
13. Session and Transaction Handling
Hibernate Session
JTA versus JDBC Transactions
Transaction handling – default pattern
JDBC transactions with ThreadLocal
JTA transaction with a single database
JDBC or JTA with the Spring framework
Conversations and Session Lifetime
Short life of a session
Lifetime until the view is rendered (Open-Session-in-View)
Long life of a session
Concurrent Access
Optimistic Locking
14. Integration with other technologies
Hibernate and Spring
Use of the Spring template
Alternative approach (nicer)
Transaction handling
Hibernate and Struts
Optimistic locking
Exception handling
IV. Configuration, Performance, Validation and Full Text Search
15. Hibernate Full Text Search
16. Performance Tuning
Analysing performance problem
Iterating through relations – batches
Iterating through relations – subqueries
Iterating through relations – single query
Reporting queries
Iterating through large resultsets
EH Cache
OS Cache
Bypass a cache
17. Configuration
Connection Pools
Built-in connection pool
A. Appendix
Annotation Reference
Entity and table annotation
Primary key annotations
Column annotations
Relation annotations
Join column annotations
Not yet described

List of Figures

7.1. Table structure
7.2. Supported Collections and Maps

List of Tables

6.1. Example of a table having a unique Integer as primary key
6.2. Example of a table having name and surname as primary key
6.3. Example of a table having a natural primary key
6.4. Id Generator Strategies
7.1. Sample hash
7.2. JPA Annotation Cascade Types
7.3. Hibernate Annotation and XML cascade types
14.1. Transaction types