Workshop on Hibernate Authors: Rely-On Solutions People : Process : Technology : Tools
Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com ©©Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Hibernate Workshop
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Agenda •Queries (Query/HQL/Criteria) •Basic Queries •Joining Associations •Advanced Queries •Spring and Hibernate •Spring basics •Spring+Hibernate •Hibernate and Struts •Integration •Struts plugin •Summary •Q&A •Feedback
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Query interfaces HQL query: SQL Query
Create a Criteria object
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Pagination Retrieve the first 10 results
Criteria Pagination. Results from 20 through 40
Execute the query to get a List
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Parameterized Queries Parameterized query string.
Multiple parameters.
Positional parameters
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Query features Using aliases.
Restriction using ‘Where’ clause
Restrictions example
Where clause using Criteria objects
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
String matching ‘SQL’ like clause.
With Criteria objects
MatchModes •START •END •ANYWHERE •EXACT
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Logical operations HQL logical OR and AND.
Same thing above in Criteria API.
Criteria using disjunction and conjunction
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Ordering Query HQL Ordering
Ordering using Criteria objects
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Joining Associations • don’t usually specify a join condition explicitly. • specify the name of a mapped Java class association.
HQL provides four ways of expressing (inner and outer) joins: •An ordinary join in the from clause •A fetch join in the from clause •A theta-style join in the where clause •An implicit association join
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Joining Associations (contd.) SQL Equivalent Fetch Joins
•Eager initialization •Outer Join instead of inner join. Lazy=“true” overridden.
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Joining Associations (contd.) Runtime Association fetching strategies • HQL always ignores the mapping document eager fetch (outer join) setting – Outer-join=“true” ignored by HQL – Criteria does not ignore it unless explicitly stated
• Only one collection eagerly • Fetching a collection Hibernate always does not garuntee unique results – Using set will ensure unique elements
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Aliases
• For inner joins use an alias to the joined class. • Returns ALL combinations of associated objects. • Collections are not initialized unlike ‘fetch’ join. •Collection of ordered pairs. Eg: (item,bid) Iterate over the collection of objects
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Aliases in HQL (contd.)
Retrieve only Items and not Bids
No ordered pairs.
How do you all these with Criteria API? © Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Aliases in HQL (contd.) Inner join using Criteria API
Inner joining using Criteria aliases
• bids not initialized •Can’t combine FetchMode.EAGER • Retrieve bids using a map
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Implicit Joins Querying components
Implicit association joining
How many joins?
NOT possible by using Criteria API © Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Theta-style joins the theta-style syntax is useful when join condition isn’t a foreign key relationship mapped to a class association.
Username not a FK in LogRecord
Results are in ordered pairs
Criteria does NOT support theta-style joins
How to get only users? © Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Subqueries
Correlated sub query
Un-Correlated sub query
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Native SQL queries Use pace-holders for value substitution
createSQLQuery is used
Named Native SQL query
Specify return types
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Spring Framework • Open-source framework created by Rod Johnson • Not limited to server-side development. Can be used to build standalone and client-server application • Lightweight – non-intrusive – application objects have no dependency on spring framework
• IOC- Inversion of control – loose coupling – runtime dependency injection
• AOP- Aspect Oriented Programming – Address cross cutting concerns – Business logic not polluted with logging, transaction etc.
Spring is a lightweight IOC and AOP container framework. © Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Spring Architecture BeanFactory- Heart of Spring core module Application Context- Adds lifecycle events, validation, internationalization Spring AOP- AOP implementation. DAO Module- DB related functionality O/R Module- Provide hooks for external ORM implementation Web-module- builds on top of application context module in context to web application MVC Framework- Springs MVC implementation, Struts and other external can also be used.
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Inversion of Control OR Dependency Injection • Core principle- “Don’t call me. I will call you.”
Direct Creation Creation through Assembler
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Hello World in Spring 1. The interface 2. Provide the Implementation
3. The Assembler xml (hello.xml)
4. The client using GreetingService
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Hibernate+Spring - goal is to separate data access and transaction demarcation aspects to allow for reusable transactional business objects that are not tied to any specific data access or transaction strategy.
Configure the JNDI datasource
Configure SessionFactory
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Hibernate+Struts 1. Extend org.apache.struts.action.PlugIn 2. Add plugin definition in struts-config.xml 3. Use in Struts action, locating it by JNDI. Code the plugin details.
Add in struts-config.xml
Look it up using JNDI.
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Hibernate+Struts
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
(contd.)
Summary
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Q&A
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com
Feedback
© Rely-On 2005; For further information on Rely-On, please visit www.rely-ongroup.com