Hibernatification! - Austin Java Users Group

Download Report

Transcript Hibernatification! - Austin Java Users Group

Hibernatification!
Roadmap for Migrating from Plain
Old SQL on JDBC to JPA on Hibernate
Duke Banerjee
Senior Developer, DrillingInfo.com
About Me
• Senior Developer at DrillingInfo since
September
• Been in the industry for almost ten years
• Been doing Java development for the past five
years
About DrillingInfo
• Subscription based website which aggregates and
organizes state regulatory data and other data in
an easy-to-use format.
• We employ many different loaders which
download data from state agencies and from
other sources.
• We have a huge database (10 GBs of data at last
count?) of information which we are always
updating and querying. Always looking for ways
of improving performance, which lead us to
Hibernate.
Overview
• In this talk, I will talk about our experiences
migrating an application from plain old SQL on
JDBC to Hibernate on JPA.
• Topics covered:
–
–
–
–
–
–
Introduction to the technologies used
Refactoring and design patterns (DAO)
Generating entity classes from the schema
Converting SQL queries to JPQL
Introduction to Guice framework
Impact of Migrating to JPA/Hibernate
What is Hibernate?
• Hibernate is an ORM (Object Relational
Mapping) tool.
• Maps the Relational Model in the database to
the Object Model in Java.
• Tables are mapped to classes.
• Columns are mapped to JavaBean properties.
• Replaces SQL with HQL, a databaseindependent query language that navigates
object relations rather than table relations.
What is JPA?
• JPA is the Java Persistence API, the entity
persistence model for EJB 3.0
• Standardized persistence framework which is
implemented by Hibernate (or TopLink, Cayenne,
etc.)
• JPA Annotations and persistence.xml provide
vendor independent configuration
• EntityManager provides vendor independent
access to persistence
• Replaces vendor specific query languages (HQL)
with standard (JPQL)
Why JPA?
• JPA is the standard, and standards are good!
• Using JPA does not tie you to Hibernate.
• JPA gives you most of the features of plain old Hibernate,
except:
– No criteria queries in JPA 2.0 
• Criteria query is a neat feature of Hibernate that constructs query
using Java-based combinators instead of alternate query language,
getting the benefit of IntelliSense and Eclipse’s refactoring tools.
– JPA doesn’t have Hibernate’s DeleteOrphan cascade type
• Delete Orphan is a useful annotation that directs Hibernate to deletes
entities in a collection if the parent is deleted, preventing orphaning.
– JPA doesn’t have an equivalent to Hibernate’s ScrollableResults
• But, all of these features are accessible to an otherwise
fully JPA application!
What is Guice?
• Guice is a very lightweight dependency
injection framework.
• Simpler to set up than a full blown IoC
container like Spring.
• Guice is focused on IoC, Spring has become an
ecosystem. Good or bad, you decide!
• Guice is XML free, configured in regular Java
code and annotations. Though Spring 3 is
advertised to do this as well.
On to the Code!
• Now that we’ve laid the groundwork, let’s see
this in action!
• I’ve create a very simple Wicket application
JPA principles introduced
which demonstrates the
so far and a migration strategy:
– Step 1: A basic JDBC and SQL driven web-app
– Step 2: Migrating to Guice-driven DAOs
– Step 3: Migrating from JDBC to JPA
What was the Point?
• Substantially reduces the amount of data
access code, partly generated but largely
managed by Hibernate. In our case, this is a
savings of thousands of lines of code.
• Better performance. Dirty checks and
versioning by Hibernate reduces the amount
of data that has to be persisted, code that we
were writing ourselves.
• IOHO, it’s just a better programming model!