Object/Relational Mapping (ORM) 2008

SIGMOD 08 Tutorial Slides (ppt) (two-hour tutorial at SIGMOD, Vancouver, June 11, 2008)
SIGMOD 08 Proceedings (6 pages) 

Hibernate/JPA and Microsoft .NET Entity Data Model (EDM): an intermediate model

This site celebrates the power of O-R Mapping to ease data modeling for practical applications and overcome the famous "impedance mismatch" between object oriented models and database data models. Between the object models and database data models there is an intermediate modeling level that can deal intelligently with both object modeling and database modeling.  This level was first implemented in Hibernate, and called the Hibernate Entity Model, at least in its graphic representation in Hibernate Tools (this view is currently disabled, unfortunately). In other words, Hibernate (similarly JPA) has an intermediate data model, without much fanfare. Microsoft has made this intermediate level explicit in their new O/R Mapping system, and calls the model the Entity Data Model. I'm calling it simply the "entity model."

Microsoft released Visual Studio .NET 2008 in December, 2007. EDM, in version 1, is available as a free addition to this IDE, as part of its Service Pack 1 (SP1, itself still in beta in July, '08).  Since EDM v1 is being released in 2008, I've titled this page with 2008. Hibernate and eclipse, the favorite IDE for its development, are as usual free. See more details linked below.

This site contains side-by-side implementations, using Hibernate and Microsoft EDM, of a simple system with four database entities related by an N-N relationship and an N-1 relationship. It is a system for ordering free pizza to be delivered to a certain room number, say in a dormitory. The "student" user has choices of pizza size and toppings, so each order has one size and a set of toppings. The "admin" can add and delete toppings and sizes, make pizzas marked baked, finish off a day and start another, etc. Although the system is simple, it is implemented with the layered architecture of larger apps, with a transactional service layer called upon by the presentation layer, the user interface code. The presentation layer is provided in two versions for each O/R mapping framework, a line-oriented UI and a web app with server-side scripting in JSP 2.0/ASP.NET. In both cases, Hibernate and EDM, the same service layer implementation works for both presentation layers.

In this simple case, the entity model is very close to the entity-relationship model, but still shows many of the core features of these very similar O-R mapping technologies. There are four entities, PizzaOrder, Topping, PizzaSize, and SysTime, the last needed to track time in days.  There is an N-N relationship between PizzaOrder and Topping and an N-1 relationship between PizzaOrder and PizzaSize.  In the physical database schema, there is a relationship table, OrderTopping, to hold (orderid, toppingid) pairs.  The N-1 relationship needs no table of its own, since it can be implemented by a foreign key in the PizzaOrder table. In the object world at the other end of the O-R mapping, there are classes PizzaOrder, Topping, PizzaSize, and SysTime for the four entities.The relationships do not themselves map to objects but rather to one or more object references for an entity object.  Here the PizzaOrder object has a collection of Topping references plus a single PizzaSize reference.

These objects are ordinary Java/C# objects, but the supporting software makes them persistent without the programmer writing any inserts, deletes or updates, or selects for primary-key lookups or full tablescans. Only more complicated queries need SQL-like treatment. These persistent objects are the bread and butter of the app, and are called domain objects because they are specific to the application domain.

The Example Pizza Shop System: public-domain sources

Hibernate Sources
Hibernate Project Setup Instructions
Hibernate Project Served by Tomcat

Microsoft EDM Sources
Microsoft EDM Setup Instructions

Some further discussion...

Development Strategies: Top down (objects first), Bottom up (database first), or Middle out

Layering of transactional applications: Presentation layer with UI (web or not), Service layer with transactions, DAO layer

The power of SQL select is mapped into the object world for retrieving views of various parts of the data.  For example, to look up the pizzas for  day 5 and room number 2, the query in Hibernate is session.createQuery("from PizzaOrder o where o.roomNumber = 5 and o.day = 2") and in EDM context.PizzaOrder.Where("it.roomNumber = 5 and it.day = 2"). In both cases the 5 and 2 can be replaced by program variables.

Acknowledgments: The pizza shop was originally a class project in my class CS636, Database Applications, at UMassBoston, in Fall '05, implemented with plain JDBC DAOs and JSPs. Thanks for their enthusiasm and insights. Also, the book Java Persistence with Hibernate, by Christian Bauer and Gavin King is extremely useful, both for getting started and running and understanding the advanced features and theoretical foundations.

Comments, fixes, etc: email Betty O'Neil: eoneil at cs.umb.edu