0% found this document useful (0 votes)
223 views24 pages

Hibernate in Practice

Hibernate in Practice - Rob Harrop Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited. Mastering some central concepts makes success much more likely. Mapping object graphs Aims - Loose coupling - High cohesion - Accurate mapping of the domain Avoid - Total navigability - Relationships to simplify queries Too coarse-grained and you risk memory usage problems and slow flushes.

Uploaded by

Gopinath
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views24 pages

Hibernate in Practice

Hibernate in Practice - Rob Harrop Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited. Mastering some central concepts makes success much more likely. Mapping object graphs Aims - Loose coupling - High cohesion - Accurate mapping of the domain Avoid - Total navigability - Relationships to simplify queries Too coarse-grained and you risk memory usage problems and slow flushes.

Uploaded by

Gopinath
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hibernate in Practice

Rob Harrop

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Introductions
• Rob Harrop
• VP, Interface21
• Core developer on Spring
• Frequent presenter (JUGs, JavaOne, JavaPolis,
OSCON...)
• Consultant with Fortune 500 clients (mainly banking)
and government
• Author of best-selling Pro Spring

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Agenda
• What you need to know to be effective
– Mapping object graphs
– Session management
– Fetching strategies
• Hibernate favourites
– Criteria API
– Testing

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
What you need to know to be effective

• Getting started with Hibernate is easy


• Unfortunately, so is getting it wrong!
• Mastering some central concepts makes success
much more likely
– Domain mapping
– Session handling
– Fetching

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Mapping object graphs
• Aims
– Loose coupling
– High cohesion
– Accurate mapping of the domain
• Avoid
– Total navigability
– Relationships to simplify queries

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Comparing Mapping Styles

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Comparing Mapping Styles

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Mapping object graphs
• Encapsulate structural logic
– Support code evolution
– Provide a cleaner API
• Avoid mapping every relationship
– Reduce coupling
– Ease maintenance

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Session Management
• Session handling can make or break an application
• Too fine-grained and you lose any performance
benefits and real transparent persistence
• Too coarse-grained and you risk memory usage
problems and slow flushes.

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Session per Operation
• Do not do this!

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Options
• Session per transaction
– Session lifecycle is synchronized with transaction boundary
– Do not use transaction per operation :)
• Long running session
– Sessions live across multiple transactions

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Issues with Long Running Sessions
• Memory usage
– Keep Sessions around in memory
• Unexpected flushes
• Manual management patterns
– Filter/ThreadLocal

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Session per transaction
• Sessions live for the duration of a transaction
• DAOs share the same session and transaction
• Sessions and transactions are managed
transparently
– Do not pass them around in method signatures
• This gives a clean unit of work within which
persistence logic can be handled

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Session per transaction

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Open Session in View
• Steer clear wherever possible
• Unclear transactional boundaries
• Potential for incorrect data
• Real potential for poor performance
– n+1 selects

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Fetch Joins
• Fetch joins allows eager loading to be selected on a
per association, per query basis
– No need to make everything eager
– No need to use OSiV
• Typically associations are lazy by default and then
eagerly fetched as needed

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Nuances with Fetch Joins
• Unexpected number of results
• The exact number of queries may surprise you

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Criteria API

• Programmatic construction of queries


• Support for all common operators
– =, <>, >, <, IN, EXISTS, BETWEEN, LIKE
• Full support for projections
• Support for custom SQL
– Projection and selection
• Additional features
– Paging, sorting, grouping

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Criteria API
• Simple query

Criteria c = [Link]([Link]);
[Link]([Link]("name", value));
return [Link]();

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Criteria API
• Query and projection

Criteria c = [Link]([Link]);
[Link](eq("name", value));
[Link](distinct(property("name")));

Static imports from Projections

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Criteria API
• Using SQL

Criteria c = [Link]([Link]);
[Link](sqlRestriction("{alias}.name = ?", value,
[Link]));
return [Link]();

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
When to use the Criteria API
• Ad-hoc querying
• Saved queries
• Conditional queries

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Testing
• Testing tools:
– DBUnit
– Spring Mock
• Testing strategy
– Test with Hibernate
– Verify with JDBC
– Work against known sets of data

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.
Useful Resources
• [Link]
• Java Persistence with Hibernate (King, Bauer)
• DBUnit - [Link]

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or


distributing without expressed written permission is prohibited.

You might also like