Java Platform, Enterprise Edition

Java EE Journal

Subscribe to Java EE Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Java EE Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

J2EE Journal Authors: Douglas Lyon, Stackify Blog, APM Blog, Sumith Kumar Puri, Javier Paniza

Related Topics: Java EE Journal, Apache Web Server Journal

J2EE Journal: Article

JDBC – The Indispensable Component of Persistence Mechanisms

A dive into the details of O/R mapping mechanisms

Few serious database applications are considered enterprise-worthy without a core database engine backed by a normalized and optimized relational database architecture. Traditionally, such database applications rely on SQL statements to retrieve and update data in the back-end data source.

This well-established model continues to have strategic importance and is the basis for the continued growth of object/relational mapping and its associated persistence mechanisms. What is object/relational mapping (O/R mapping)? It's a programming technique that links a relational database to object-oriented language concepts. O/R mapping mechanisms allow a developer to create Java objects using an object-oriented perspective, eliminating the impedance mismatch that exists between object and relational models.

O/R mapping is a compelling solution for you, as a Java developer, because you can concentrate on building the application while leaving the data persistence details to the O/R mapping mechanism.

As a Java developer, you have a variety of choices when it comes to Java-based O/R mapping mechanisms. Three communities or organizations are most active in the Java O/R persistence world: open source communities; standards-based organizations, and commercial ventures.

Open source includes prominent implementations such as Hibernate and the Spring Framework. Standards-based implementations are technologies such as EJB 3.0 (JSR-220) and JDO (JSR-244). Commercial implementations of note include Oracle's TopLink. In this article, we take a look at the open source and standards-based options to understand the specific nuances that you should consider when selecting an O/R mapping mechanism for your environment.

Regardless of the O/R mapping mechanism, they all leverage JDBC to communicate with the underlying relational database. Although it is possible to use an O/R mapping mechanism to access non-relational data sources, the vast majority of applications leverage relational databases. With that in mind, you need to carefully consider each layer in the software stack to ensure an optimal O/R persistence design. As you'll see, each O/R mapping mechanism has a singular dependency on the JDBC driver to communicate data to and from the database in a highly efficient manner. If a suboptimal JDBC driver is used, gains in developer efficiencies are not complemented by a fast-performing, highly scalable application. Selecting the best-performing, most reliable JDBC driver is essential for building and deploying your applications on any O/R mapping mechanism.

Figure 1 shows a representation of the various O/R mapping mechanisms and how they relate to the application code and relational data sources. This clearly illustrates the critical role fulfilled by the JDBC driver since it is used as the foundation for each of these O/R mapping mechanisms. The efficiency of the JDBC driver has a profound effect on the performance, scalability, and reliability of the applications. Each O/R mapping mechanism is fully dependent on the performance of the JDBC driver, regardless of the design of the O/R mapping mechanism API exposed to the application code, and regardless of the SQL-based optimizations the O/R mapping mechanism is able to achieve.

One of the most hotly debated and passionate discussions in the software industry is which O/R mapping mechanism deserves to be the dominant choice. This discussion has left the Java community polarized. As this debate rages on, developers and architects should select the O/R mapping mechanism that best fits the needs of their applications. This goes for each component in the architecture stack, including selecting the best JDBC driver in order to realize application success.

Before we delve into the details of O/R mapping mechanisms, it's important to acknowledge that O/R technologies do not alleviate the need for applications that are built directly on top of the JDBC (JSR 054 and JSR 221) and JDBC RowSet Implementations (JSR 114). In many cases, these technologies will continue to be an optimal solution and will continue to be useful for many practical purposes. JavaServer Faces, JSP, and JSP Tab Libraries have considerable support for directly binding Web interface widgets to underlying relational structures, allowing developers to quickly and easily develop applications. A number of ease-of-development enhancements are being made that will allow organizations to continue to leverage the investments that they have made in these important technologies.

To navigate our way through the stormy waters of O/R mapping mechanisms, let's take a look at the most active open source and standards-based implementations. As we step through these mechanisms, I will describe their high-level features and highlight how many of the common features of these sometime competing technologies can be used in a complementary fashion.

Open Source: Hibernate and Spring
Hibernate is arguably considered the leading open source O/R mapping mechanism. Its success can be attributed to a vibrant developer community, which is widely considered to be a critical life source for any successful and prominent open source project. Hibernate provides a well-designed API that permits full persistence within the Java Object model, encompassing support for collections, inheritance, and polymorphism. For developer's considering O/R persistence for the first time, Hibernate provides the option to access the JDBC API directly, allowing greater flexibility since you can leverage the full power of SQL. Hibernate's lead developers regard this as an important asset, especially for those organizations that have made significant investments in relational database technologies.

Hibernate 2.0 is a production ready, fairly mature solution that provides the base capabilities required for O/R mapping. With the Hibernate 3.x releases, the APIs and the overall look and feel of the framework is evolving to look more like the APIs documented in the EJB 3.0 specification. The most significant contribution that Hibernate has made to EJB 3.0 design is the EntityManager, which manages the life cycle of entities or objects. This core functional component, originally inspired by Hibernate, has been incorporated into the EJB 3.0 specification and will soon become one of the standard interfaces for the upcoming Java EE 5.0 platform.

With the convergence in the overall design of Hibernate and EJB 3.0, some would argue that the future of Hibernate is bleak. After all, many of the improved interfaces, annotations, and ease-of-development considerations included in EJB 3.0 are the direct result of three iterations of the Hibernate product. I would argue that by aligning the Hibernate interfaces with EJB 3.0, Hibernate has nicely positioned itself for wide-range adoption outside of the Java EE 5.0 standard. In addition, it remains in a position to innovate and introduce bleeding-edge enhancements outside the context of a standards body.

In the following example, let's look at how to configure Hibernate with the DataDirect Connect for JDBC driver for the Microsoft SQL Server database. As with most JDBC-enabled applications, Hibernate permits configuration with a JDBC Connection or DataSource. First, locate the $HIBERNATE_HOME/etc/ file and insert the following configuration details:

hibernate.connection.driver_class = com.ddtek.jdbc.sqlserver.SQLServerDriver
hibernate.connection.url = jdbc:datadirect:sqlserver://server_name:1433
hibernate.connection.username = sa
hibernate.connection.password = sa

Next, Hibernate needs to be configured to use the correct SQL dialect. In this case, we need to configure Hibernate to function with SQL Server.

hibernate.dialect org.hibernate.dialect.SQLServerDialect

More Stories By Jonathan Bruce

Jonathan Bruce is program manager at DataDirect Technologies. He has led and participated in four JSRs and enjoys helping Java and .NET developers take advantage of the benefits XQuery offers when working with XML and a variety of databases. Recently relocated from San Fransisco to North Carolina, Jonathan spends his weekends running, sailing, and traveling.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
Tony Walker 11/16/05 09:40:15 AM EST

Not a bad article, but it's a pity you didn't include JDO and some of the newer ideas like db4o and Persistent Java Objects in the evaluation.

JDO is a standards-based direct competitor to Hibernate, with many implementations to choose from.

Persistent Java Objects is a new system well suited to RAD. It requires no mapping, and so is very fast to implement and easy to modify.

JDJ News Desk 11/01/05 03:53:17 PM EST

JDBC -The Indispensable Component of Persistence Mechanisms. Few serious database applications are considered enterprise-worthy without a core database engine backed by a normalized and optimized relational database architecture. Traditionally, such database applications rely on SQL statements to retrieve and update data in the back-end data source.