Amazon DevCon – Gavin King
Gavin King leads the Hibernate project at JBoss. Gavin said he planned to talk about EJB 3.0. It’s conceptually similar to what has been in Hibernate 1.0 and 2.0 for quite a while. Gavin told us that he would segue from EJB 3.0 into talking about Hibernate 3.0.
Motivation for EJB 3.0: Simplified programming model (make middleware continue to be successful as a line of porducts – simplified APIs or no APIs at all). EJB 2.1 is too “noisy” (too much code was required to make container happy; slower to write, maintain, and understand). Metadata is XML hell (it was code itself and wasn’t easily maintained). Simplify testability – unit testing and automation, test suites that are run frequently (hundreds of times a day). Simplified application architecture. Simplify modeling by giving you a richer language to work with (domain object modeling). Simplify querying by making the query language more powerful (EJBQL required many workarounds if you wanted to do anything interesting. You had to break out of EJB model).
Entity beans in EJB 3
– Plain Java Objects: no required interfaces/inheritance, all public methods are non-final, all instance variables are private, non-abstract persistent properties, etc.
– Metadata is as important: Mapped as annotations. Changes more often when you’re maintaining a domain model. There are two levels of annotations: logical level and schema level.
Gavin showed some code for creating an entity bean. He said some people think annotations are really ugly and don’t like seeing SQL stuff embedded in code. Gavin said annotations will soon be created automatically.
You can just instantiate entities since they’re plain objects.
No more home interface (“detyped ‘home’ interface”), but home interfaces can be useful for running queries and creating objects. They had to replace it with something. The new operator instantiates an entity. Managed entities are associated with an EntityManager. New entities become managed by calling create(). Entities become detached when a persistence context ends. The state of detached entities may be reused in a new persistence context using merge(). This provides true support for optimistic transactions.
Nice to make a typed interface that isolates business-specific stuff.
Queries can be expressed as EJBQL strings. Can be invoked via the Query API.
EJBQL 3.0 is very similar to HQL (Hibernate Query Language). Aggregation, projection, fetching, subselects. Minimal changing to standard SQL to make it object-oriented. Definition of what data I’m interested in — what data do I need in this transaction? This is how one problem between object-relational (O/R) mismatch is addressed.
What more is coming? Session beans, interceptors and callback listeners, native SQL query support, XML-based O/R mapping (this will be an alternative if you don’t like annotations).
Hibernate has been around for three years. 3.0 is a major revision of Hibernate implementation and APIs. Migration from 2.X is straightforward, but it’s not a drop-in replacement. 3.0 fixes several limitations.
Hibernate 3.0 forms the core implementation of JBoss EntityManager (EJB 3.0 – preview is available now). Going live in a month.
Coolest new feature is filters. The motivation behind filters is looking at temporal, permissioned, and regional data. Global, parameterized restriction on the data. Doing this in SQL is difficult. Filter conditions may be defined as SQL fragments in a mapping document, and they may be applied cumulatively. Filters offer a simple declarative mechanism for handling messy queries.
Filters are defined in mapping metadata. You can define what the filter means for a particular entity. The filter is then injected into any join conditions and where clauses for that entity. Filters are applied transparently when query runs.
Handwritten SQL support. This is an improved feature in Hibernate 3.0. Motivation is that writing SQL is tedious and generated SQL is a good thing. You can bind SQL queries to managed persistent objects.
Gavin showed some code samples to show this in action.
More new Hibernate 3.0 features:
- Many new O/R mapping features
- Support for non-POJO domain models
- JMX-based reporting
- More at http://hibernate.org/About/RoadMap