Title: Java Persistence Frameworks
1Java Persistence Frameworks
- Patrick W. McMichael
- J2EE Architect, Pillar Technology Group
2Introduction
- Background
- Data Access a necessary evil
- Time to Code
- Time to Test
- Time to Tune
- Purpose and Scope
- Historical Perspective on Various Alternatives
- Primary Focus on Hibernate and EJB 3.0
3Agenda
- Part I A Little History
- JDBC
- Pre-EJB 3.0 CMP Entity Beans
- JDO/Hibernate
- EJB 3.0 Entity Beans
- Part II A Side-By-Side Comparison
- Hibernate vs. EJB 3.0 Entity Beans
- General Approach
- CRUD Operations
- Cross-object Relationships
- Transactions
- Part III Where Do I Go From Here?
- Similarities
- Important Distinctions
4 5Howd we get here? -- JDBC
- Call-level DB access via SQL
- This has traditionally been the comfort zone
for many developers - Leaves developers fully in control
- BUTrequires manual, relatively tedious work
6Howd we get here? -- JDBC
- Basic Steps
- Obtain a connection to the DB
- Use the connection to create a statement or to
access a stored procedure - Execute the Statement
- In the case of queries
- Iterate through each row of data in the result
set - For each row, parse the data into the appropriate
domain objects
7Howd we get here? -- Along Came CMP
- ORM object relational mapping via XML DD
- Container-managed data access vs. manual SQL
- EJB 2.0 added some great features
- Local Interfaces
- CMR container-managed relationships
- EJB QL it took until 2.1 to get a decent subset
of SQL operations represented
8Howd we get here? -- Along Came CMP
- Pre-EJB 3.0 CMP Challenges
- RMI style access addressed via EJB 2.0 local
interfaces - Poor performance -- most often due to abuse of
Entity Beans - Massive pool sizes
- Jiffy-Pop syndrome
- Inherent dependency upon the EJB container
- Impacts testability
- Impacts reuse in lightweight alternate
deployments - Convoluted class/interface structure
- NOT POJO-centric forced use of Session Facade
- No support for inheritance
- In general, felt like a departure from OO
- Limited query capabilities (EJB 2.1 EJB QL
enhancements helped) - Custom finders handled via DD limits ability to
make query dynamic at request time (e.g. search
w/ a broad set of possible criteria)
9Howd we get here? JDO/Hibernate
- POJO-Centric Model
- No entity-to-DTO conversion games
- NOT container-dependent
- Facilitates multiple deployment scenarios
- Faciliates out-of-container testing
- JDO
- Standards-based (JSR 12/243 for 1.x/2.0 versions)
- Bytecode processing approach
- Hibernate
- Proprietary (though open-source)
- Uses runtime reflection/CGLIB modification
10Howd we get here? EJB 3.0 Entity Beans
- Plagiarism or Flattery?
- Now POJO-centric
- No longer container-dependent
- Convoluted interface structure done away with
- EJB QL enhancements
- Dynamic query capabilities
11Howd we get here? Summary
- Round One allow SQL access to DB
- Round Two layer of abstraction / ORM
- Round Three POJO-centric response to Entity
Beans - Round Four Lessons learned applied
- Seeking to get back to a single core
specification for Java persistence
12- PART II Side-by-Side Comparison
13Side-By-Side Comparison Cool Mixins
14General Approach -- Hibernate
- POJO-centric model
- Key Interfaces
- SessionFactory like DataSource in JDBC
- Session like Connection in JDBC
- Query means of executing HQL (Hibernate Query
Language) - Transaction for programmatic demarcation
- Instance States
- Transient never persisted via a Session
- Persistent associated with an active Session
- Detached previously persisted, but not
currently associated with an active Session
15General Approach -- Hibernate
- ORM
- Typically handled via XML (.hbm.xml file next to
POJO) - XDoclet may be used to generate these
- Support in progress for JDK 1.5 style annotations
- Example CreationDto.hbm.xml
- Referencing mapping files
- Programmatically during Configuration/SessionFacto
ry setup - In hibernate.cfg.xml
- Via Spring XML file (my preference!)
- Mapping files can also be used to
- Generate DDL
- Generate the POJO instances
16General Approach EJB 3.0 Entity Beans
- Now a POJO-centric model too
- Key Interfaces
- EntityManagerFactory like SessionFactory in
Hibernate - EntityManager like a Session in Hibernate
- Query like its namesake in Hibernate
- EntityTransaction for programmatic (resource
local) demarcation
17General Approach EJB 3.0 Entity Beans
- Persistence Context
- Lifecycle in container determined by JTA
transaction in place - EXCEPT when coordinated with Stateful Session
Beans, where its lifecycle may span JTA
transactions (extended P.C.) - Outside the container, it is tightly bound to the
lifecycle of the EntityManager - Instance States
- New like Transient in Hibernate
- Managed like Persistent in Hibernate
- Detached previously persisted, but not
currently associated with an active Session - Removed Currently associated with a persistence
context, but scheduled for removal from the DB
18General Approach EJB 3.0 Entity Beans
- ORM
- Typically handled via JDK 1.5 style annotations
- May be specified via XML files
- Example CreationDto.java
- Obtaining an EntityManager
- JNDI lookups
- Dependency Injection (J2EE)
- _at_PersistenceContext
- EntityManager em
- getEntityManager on EntityManagerFactory (J2SE)
- EntityManagerFactory emf
- Persistence.createEntityManagerFactory()
- EntityManager em emf.createEntityManager()
19CRUD Operation Examples -- Hibernate
- createCreation()
- session.save()
- findCreationsByIceCreamFlavor()
- Using Criteria instance
- very object-centric approach
- findCreationsByMixin()
- Example of named query
- Using HQL
20CRUD Operation Examples EJB 3.0
- createCreation()
- em.persist()
- findCreationsByIceCreamFlavor()
- Using EJB QL
- findCreationsByMixin()
- Example of named query
- Using EJB QL
21Cross-Object Relationships -- Hibernate
- Example mixins property of Creation object
- many-to-many relationship
- Associative table CREATION_MIXIN
- key-column CREATION_ID
- Column for id of related entity MIXIN_ID
- ltbag name"mixins" table"CREATION_MIXIN"
cascade"none"gt - ltkey column"CREATION_ID"/gt
- ltmany-to-many column"MIXIN_ID"
- class"com.coolmixins.hibernate.dto.Mi
xinDto"/gt - lt/baggt
22Cross-Object Relationships EJB 3.0
- Annotation Examples for CreationDto.java
- Example _at_ManyToOne properties
- Inventor
- Ice Cream
- Example _at_ManyToMany property
- _at_JoinTable usage
- _at_JoinColumn name/referenced column name
23Transactions -- Hibernate
- In-container
- Seamlessly ties into CMT
- Using just the open and close calls on the
session - Out-of-container
- Session.beginTransaction()
- Transaction interface (commit/rollback)
24Transactions EJB 3.0 Entity Beans
- In-container
- Seamlessly ties into CMT
- e.g. Session Façade with JTA transaction in place
- Out-of-container
- EntityManager.getTransaction()
- EntityTransaction interface (begin/commit/rollback
)
25- PART III Where do I go from here?
26Where do I go from here?
- Hibernate (pre-3.1) / EJB 3.0 Entity Bean
similarities - Both meet todays key persistence framework
demands - In/Out of container usage with transactional
support - POJO-centric, OO friendly model
- Automation of data storage/retrieval
- HQL and EJB QL in EJB 3.0 both provide rich query
capabilities
27Where do I go from here?
- Important Points of Distinction (aka Wait One
Slide) - Hibernate is proprietary
- EJB 3.0 Entity Beans are based on a JCP-driven
spec - Hibernate is available NOW
- EJB 3.0 Entity Beans are still a work in progress
(though beta/preview versions are available) - Hibernate (pre-3.1) has been largely focused on
the external XML approach to ORM - EJB 3.0 Entity Beans focus on annotations as the
main approach - ? What happens when the same resource needs to
be configured in different ways in different
contexts (e.g. legacy and enterprise mappings of
the same POJO) - Hibernate is offering Annotations and EJB 3.0
style methods - EJB 3.0 is slated to allow XML ORM as an
alternative - This should boil down to CHOICE for developers
28Where do I go from here?
- A convergence in the force
- Finallythis is really welcome after the whole
JDO vs. EJB 3.0 fiasco - Hibernate 3.0.x started adding EJB 3.0 style
methods (e.g. persist and merge) - Preview releases (JBoss, built on top of
Hibernate), Oracle (built upon TopLink) - Hibernate 3.1/Annotations used by JBoss, for
example - JBoss PAR files can use EJB 3.0 annotatations
- They can also tie in to .hbm.xml ORM files
- The EJB 3.0 Hibernate Fallacy --
http//www.jroller.com/comments/mkeith/Weblog/the_
ejb_3_0_hibernate - BEA Acquisition of SolarMetric (Kodo JDO / EJB
3.0) - Slated for WLS 9.5