« CodeFutures Support Weblog | Main | FireStorm/DAO 2.4 GA »

Data Access Object (DAO) versus Object Relational Mapping (ORM)

Following on from my posting Choosing a Java Persistence Strategy I decided to blog further on the topic of persistence choices that a developer must make. In this posting I will give an overview of Data Access Objects (DAO) and Object Relational Mapping (ORM) and talk about some of the differences between them.

Data Access Object (DAO)

DAO offers a very simple method of mapping Java objects to Databases. To build a Customer DAO a developer would write a Customer class which contains one attribute for each field in the customer table, and a CustomerDao class which contains methods for inserting, updating, selecting, and deleting customer rows. The CustomerDao class would typically contain JDBC code for executing the necessary SQL statements.

The DAO design pattern has the following advantages:

• Simple – can be understood by most Java developers
• Separation of data (DTO) and behaviour (DAO)
• Designed for distributed architectures (DTO classes can be passed between tiers and DAO classes can be exposed as J2EE Session Beans or Web Services)
• No runtime container required (DAO code can be unit tested on the client)
• Can result in highly efficient code if DAO code is designed to take advantage of database capabilities (stored procedures, joins, etc).

The DAO design pattern has the following disadvantages:

• Requires large volumes of implementation code to be written (unless a DAO Code Generator is being used)
• Writing code to navigate the object model requires some understanding of the database schema

Object Relational Mapping (ORM)

There are many ORM tools on the market and they provide a number of different capabilities and coding styles. Examples of ORM technologies/products include EJB, JDO and Hibernate.

When using an ORM approach it is quite common for the Java object model to be developed first (possibly using a UML tool), with relationships between classes modeled as Collections. The mapping between the Java classes and the database tables are modeled using a graphical tool and stored in configuration files that will be used by the ORM runtime.

The ORM approach has the following advantages:

• Java domain model uses natural Java programming style (uses Collections and Iterators to navigate relationships)
• ORM runtime can provide capabilities such as Caching, Auditing that would have to be hand-coded using the DAO approach

The ORM approach has the following disadvantages:

• Data and behaviour are not separated
• Façade needs to be built if the data model is to be made available in a distributed architecture
• Each ORM technology/product has a different set of APIs and porting code between them is not easy

Using DAO with ORM

It is possible to use both DAO and ORM within the same project (for instance, a DAO tier could be provided to make data available to remote clients). It is also possible to build (either by hand or using a code generator) a DAO façade onto an ORM domain model, giving the benefits of the ORM runtime and the distributed capabilities of the DAO design pattern.

Summary

DAO and ORM are both valid approaches to persistence and the choice between them needs to be considered on a per-project basis.

Resources:

Comments

One of the disadvantages that you state r.e. ORM is "Façade needs to be built if the data model is to be made available in a distributed architecture". That's actually not true of many ORM products, notably any products supporting JDO1 (makeTransient), JDO2 detach/attach (which actually provides additional capabilities, and Hibernate. When using those ORM products, your POJOs (the domain objects) can be transferred between tiers.

One disadvantage you state with DAO's is the need to write large volumes of code. While I don't consider myself an expert on this subject, I would be suspect of any organisation that has a db with more than say, 5 tables, and is not using some form of scripting to generate the DAO's. Also, understanding the db schema can be a lot easier than understandingthe object model (and vice versa of course ..).
Thoughts ?

Hi Andy,

We are very interested in FireStorm/DAO. One single thing concerns us: if and how can FireStorm generate join between tables?

Thanks in advance,

Sherban

FireStorm/DAO offers a Custom DAO feature that allows you to expose any SQL statement as a DAO class. This can include multi-table joins as well as bulk update/delete statements.

There is an FAQ entry at:

/products/firestorm/faq/jdbc/complexqueries.html

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)