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
Introduction
Creating Java Project and classes
Hibernate configuration
Annotation or XML
Mapping
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
Saving
Updating
Deleting
Additional commands
EntityManager API
Saving
Updating
Deleting
Additional commands
4. A more complex example – web application
Summary
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
Cascading
1:1 relation
1:n
m:n
1:n:1
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
PostgreSQL
Oracle
11. Querying data
Useful tools
Beam me into the code
JBoss Tools
Squirrel SQL Client
HQL
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
NamedQuery
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
Configuration
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
Caches
General
EH Cache
OS Cache
Swarmcache
Bypass a cache
17. Configuration
Connection Pools
Built-in connection pool
JNDI
A. Appendix
Annotation Reference
Entity and table annotation
Primary key annotations
Column annotations
Special
Relation annotations
Join column annotations
Components
Inheritance
Queries
Not yet described
Index

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