Understanding object/relational persistence This chapter covers Object persistence with SQL databases The object/relational paradigm mismatch Persistence layers in object-oriented applications Object/relational mapping basics
1 Understanding object/relational persistence This chapter covers ■ Object persistence with SQL databases ■ The object/relational paradigm mismatch ■ Persistence layers in object-oriented applications ■ Object/relational mapping basics Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
CHAPTER1 The approach to managing persistent data has been a key design decision in every software project we've worked on.Given that persistent data isn't a new or unusual requirement for Java applications,you'd expect to be able to make a simple choice among similar,well-established persistence solutions.Think of web application frameworks (Jakarta Struts versus WebWork).GUI component frameworks(Swing versus SWT).or template engines (JSP versus Velocity).Each of the competing solutions has advantages and disadvantages,but they at least share the same scope and overall approach.Unfortunately.this isn't yet the case with persistence tech- nologies,where we see some wildly differing solutions to the same problem. For several years,persistence has been a hot topic of debate in the Java com munity.Many developers don't even agree on the scope of the problem.Is"per. sistence"a problem that is already solved by relational technology and extensions such as stored procedures,or is it a more pervasive problem that must be addressed by special Java component models such as EB entity beans?Should we hand-code even the most primitive CRUD (create,read,update,delete)opera- tions in SQL and JDBC.or should this work be automated?How do we achieve portability if every database management system has its own SQL dialect?Should we abandon SQL completely and adopt a new database technology.such as object database systems?Debate continues,but recently a solution called object relational mapping(ORM)has met with increasing acceptance.Hibernate is an open source ORM implementation. Hibernate is an ambitious project that aims to be a complete solution to the problem of managing persistent data in Java.It mediates the application's interac. tion with a relational database,leaving the developer free to concentrate on the business problem at hand.Hibernate is an non-intrusive solution.By this we mean you aren't required to follow many Hibernate-specific rules and design patterns when writing your business logic and persistent classes:thus.Hibernate integrates smoothly with most new and existing applications and doesn't require disruptive changes to the rest of the application. This book is about Hibernate.We'll cover basic and advanced features and describe some recommended ways to develop new applications using Hibernate. Often,these recommendations won't be specific to Hibernate-sometimes they will be our ideas about the best ways to do things when working with persistent data,explained in the context of Hibernate.Before we can get started with Hiber- nate,however.you need to understand the core problems of object persistence and object/relational mapping.This chapter explains why tools like Hibernate are needed
2 CHAPTER 1 Understanding object/relational persistence The approach to managing persistent data has been a key design decision in every software project we’ve worked on. Given that persistent data isn’t a new or unusual requirement for Java applications, you’d expect to be able to make a simple choice among similar, well-established persistence solutions. Think of web application frameworks (Jakarta Struts versus WebWork), GUI component frameworks (Swing versus SWT), or template engines (JSP versus Velocity). Each of the competing solutions has advantages and disadvantages, but they at least share the same scope and overall approach. Unfortunately, this isn’t yet the case with persistence technologies, where we see some wildly differing solutions to the same problem. For several years, persistence has been a hot topic of debate in the Java community. Many developers don’t even agree on the scope of the problem. Is “persistence” a problem that is already solved by relational technology and extensions such as stored procedures, or is it a more pervasive problem that must be addressed by special Java component models such as EJB entity beans? Should we hand-code even the most primitive CRUD (create, read, update, delete) operations in SQL and JDBC, or should this work be automated? How do we achieve portability if every database management system has its own SQL dialect? Should we abandon SQL completely and adopt a new database technology, such as object database systems? Debate continues, but recently a solution called object/relational mapping (ORM) has met with increasing acceptance. Hibernate is an open source ORM implementation. Hibernate is an ambitious project that aims to be a complete solution to the problem of managing persistent data in Java. It mediates the application’s interaction with a relational database, leaving the developer free to concentrate on the business problem at hand. Hibernate is an non-intrusive solution. By this we mean you aren’t required to follow many Hibernate-specific rules and design patterns when writing your business logic and persistent classes; thus, Hibernate integrates smoothly with most new and existing applications and doesn’t require disruptive changes to the rest of the application. This book is about Hibernate. We’ll cover basic and advanced features and describe some recommended ways to develop new applications using Hibernate. Often, these recommendations won’t be specific to Hibernate—sometimes they will be our ideas about the best ways to do things when working with persistent data, explained in the context of Hibernate. Before we can get started with Hibernate, however, you need to understand the core problems of object persistence and object/relational mapping. This chapter explains why tools like Hibernate are needed. Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
What is persistence? 3 First,we define persistent data management in the context of object-oriented applications and discuss the relationship of SQL.JDBC.and Java,the underlying technologies and standards that Hibernate is built on.We then discuss the so- called object/relational paradigm mismatch and the generic problems we encounter in object-oriented software development with relational databases.As this list of prob- lems grows,it becomes apparent that we need tools and patterns to minimize the time we have to spend on the persistence-related code of our applications.After we look at alternative tools and persistence mechanisms,you'll see that ORM is the best available solution for many scenarios.Our discussion of the advantages and drawbacks of ORM gives you the full background to make the best decision when picking a persistence solution for your own project. The best way to learn isn't necessarily linear.We understand that you probably want to try Hibernate right away.If this is how you'd like to proceed,skip to chapter 2,section 2.1."Getting started,"where we jump in and start coding a (small)Hibernate application.You'll be able to understand chapter 2 without reading this chapter.but we also recommend that you return here at some point as you circle through the book.That way,you'll be prepared and have all the back- ground concepts you need for the rest of the material. 1.1 What is persistence? Almost all applications require persistent data.Persistence is one of the funda mental concepts in application development.If an information system didn't pre- serve data entered by users when the host machine was powered off,the system would be of little practical use.When we talk about persistence in Java,we're nor- mally talking about storing data in a relational database using SQL.We start by tak ing a brief look at the technology and how we use it with Java.Armed with that information,we then continue our discussion of persistence and how it's imple mented in object-oriented applications. 1.1.1 Relational databases You,like most other developers,have probably worked with a relational database In fact,most of us use a relational database every day.Relational technology is a known quantity.This alone is sufficient reason for many organizations to choose it.But to say only this is to pay less respect than is due.Relational databases are so entrenched not by accident but because they're an incredibly flexible and robust approach to data management
What is persistence? 3 First, we define persistent data management in the context of object-oriented applications and discuss the relationship of SQL, JDBC, and Java, the underlying technologies and standards that Hibernate is built on. We then discuss the socalled object/relational paradigm mismatch and the generic problems we encounter in object-oriented software development with relational databases. As this list of problems grows, it becomes apparent that we need tools and patterns to minimize the time we have to spend on the persistence-related code of our applications. After we look at alternative tools and persistence mechanisms, you’ll see that ORM is the best available solution for many scenarios. Our discussion of the advantages and drawbacks of ORM gives you the full background to make the best decision when picking a persistence solution for your own project. The best way to learn isn’t necessarily linear. We understand that you probably want to try Hibernate right away. If this is how you’d like to proceed, skip to chapter 2, section 2.1, “Getting started,” where we jump in and start coding a (small) Hibernate application. You’ll be able to understand chapter 2 without reading this chapter, but we also recommend that you return here at some point as you circle through the book. That way, you’ll be prepared and have all the background concepts you need for the rest of the material. 1.1 What is persistence? Almost all applications require persistent data. Persistence is one of the fundamental concepts in application development. If an information system didn’t preserve data entered by users when the host machine was powered off, the system would be of little practical use. When we talk about persistence in Java, we’re normally talking about storing data in a relational database using SQL. We start by taking a brief look at the technology and how we use it with Java. Armed with that information, we then continue our discussion of persistence and how it’s implemented in object-oriented applications. 1.1.1 Relational databases You, like most other developers, have probably worked with a relational database. In fact, most of us use a relational database every day. Relational technology is a known quantity. This alone is sufficient reason for many organizations to choose it. But to say only this is to pay less respect than is due. Relational databases are so entrenched not by accident but because they’re an incredibly flexible and robust approach to data management. Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
CHAPTER 1 Understanding object/relational persistenc A relational database management system isn't specific to Java,and a relational database isn't specific to a particular application.Relational technology provides a way of sharing data among different applications or among different technologies that form part of the same application(the transactional engine and the reporting engine.for example).Relational technology is a common denominator of many disparate systems and technology platforms.Hence,the relational data model is often the common enterprise-wide representation of business entities. Relational database management systems have SQL-based application program ming interfaces;hence we call today's relational database products SoL database management systems or.when we're talking about particular systems,SoL databases. 1.1.2 Understanding SQL To use Hibernate effectively,a solid understanding of the relational model and SQL is a prerequisite.You'll need to use your knowledge of SQL to tune the per- formance of your Hibernate application.Hibernate will automate many repetitive coding tasks,but your knowledge of persistence technology must extend beyond Hibernate itself if you want take advantage of the full power of modern SQL data- bases.Remember that the underlying goal is robust,efficient management of per- sistent data. Let's review some of the SOL terms used in this book.You use SQL as a data def inition language (DDL)to create a database schema with CREATE and ALTER state- ments.After creating tables (and indexes,sequences,and so on),you use SQL as a data manipulation language(DML).With DML,you execute SQL operations that manipulate and retrieve data.The manipulation operations include insertion, update and deletion.You retrieve data by executing queries with restriction,projection. and join operations (including the Cartesian produch).For efficient reporting.you use SQL to group.order,and aggregate data in arbitrary ways.You can even nest SQ statements inside each other:this technique is called subselecting.You have proba- bly used SQL for many years and are familiar with the basic operations and state ments written in this language.Still,we know from our own experience that SQL is sometimes hard to remember and that some terms vary in usage.To understand this book,we have to use the same terms and concepts;so,we advise you to read appendix A if any of the terms we've mentioned are new or unclear SQL knowledge is mandatory for sound Java database application development If you need more material,get a copy of the excellent book SoL Tuningby Dan Tow [Tow 2003].Also read An Introduction to Database Systems [Date 2004]for the theory concepts,and ideals of (relational)database systems.Although the relational
4 CHAPTER 1 Understanding object/relational persistence A relational database management system isn’t specific to Java, and a relational database isn’t specific to a particular application. Relational technology provides a way of sharing data among different applications or among different technologies that form part of the same application (the transactional engine and the reporting engine, for example). Relational technology is a common denominator of many disparate systems and technology platforms. Hence, the relational data model is often the common enterprise-wide representation of business entities. Relational database management systems have SQL-based application programming interfaces; hence we call today’s relational database products SQL database management systems or, when we’re talking about particular systems, SQL databases. 1.1.2 Understanding SQL To use Hibernate effectively, a solid understanding of the relational model and SQL is a prerequisite. You’ll need to use your knowledge of SQL to tune the performance of your Hibernate application. Hibernate will automate many repetitive coding tasks, but your knowledge of persistence technology must extend beyond Hibernate itself if you want take advantage of the full power of modern SQL databases. Remember that the underlying goal is robust, efficient management of persistent data. Let’s review some of the SQL terms used in this book. You use SQL as a data definition language (DDL) to create a database schema with CREATE and ALTER statements. After creating tables (and indexes, sequences, and so on), you use SQL as a data manipulation language (DML). With DML, you execute SQL operations that manipulate and retrieve data. The manipulation operations include insertion, update, and deletion. You retrieve data by executing queries with restriction, projection, and join operations (including the Cartesian product). For efficient reporting, you use SQL to group, order, and aggregate data in arbitrary ways. You can even nest SQL statements inside each other; this technique is called subselecting. You have probably used SQL for many years and are familiar with the basic operations and statements written in this language. Still, we know from our own experience that SQL is sometimes hard to remember and that some terms vary in usage. To understand this book, we have to use the same terms and concepts; so, we advise you to read appendix A if any of the terms we’ve mentioned are new or unclear. SQL knowledge is mandatory for sound Java database application development. If you need more material, get a copy of the excellent book SQL Tuning by Dan Tow [Tow 2003]. Also read An Introduction to Database Systems [Date 2004] for the theory, concepts, and ideals of (relational) database systems. Although the relational Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
What is persistence? 5 database is one part of ORM,the other part,of course,consists of the objects in your Java application that need to be persisted to the database using SQL. 1.1.3 Using SQL in Java When you work with an SQL database in a Java application,the Java code issues SQL statements to the database via the Java DataBase Connectivity (JDBC)API.The SOL itself might have been written by hand and embedded in the Java code,or it might have been generated on the fly by Java code.You use the JDBC API to bind arguments to query parameters,initiate execution of the query,scroll through the query result table,retrieve values from the result set,and so on.These are low level data access tasks;as application developers,we're more interested in the business problem that requires this data access.It isn't clear that we should be concerning ourselves with such tedious,mechanical details. What we'd really like to be able to do is write code that saves and retrieves com plex objects-the instances of our classes-to and from the database,relieving us of this low-level drudgery. Since the data access tasks are often so tedious,we have to ask:Are the relational data model and (especially)SQL the right choices for persistence in object- oriented applications?We answer this question immediately:Yes!There are many reasons why SQL databases dominate the computing industry.Relational database management systems are the only proven data management technology and are almost always a requirement in any Java project. However,for the last 15 years,developers have spoken of a paradigm mismatch. This mismatch explains why so much effort is expended on persistence-related concerns in every enterprise project.The paradigms referred to are object model ing and relational modeling.or perhaps object-oriented programming and SQL Let's begin our exploration of the mismatch problem by asking what persistence means in the context of object-oriented application development.First we'll widen the simplistic definition of persistence stated at the beginning of this section to a broader,more mature understanding of what is involved in maintaining and using persistent data. 1.1.4 Persistence in object-oriented applications In an object-oriented application.persistence allows an object to outlive the pro- cess that created it.The state of the object may be stored to disk and an object with the same state re-created at some point in the future. This application isn't limited to single objects-entire graphs of interconnected obiects may be made persistent and later re-created in a new process.most obiects
What is persistence? 5 database is one part of ORM, the other part, of course, consists of the objects in your Java application that need to be persisted to the database using SQL. 1.1.3 Using SQL in Java When you work with an SQL database in a Java application, the Java code issues SQL statements to the database via the Java DataBase Connectivity (JDBC) API. The SQL itself might have been written by hand and embedded in the Java code, or it might have been generated on the fly by Java code. You use the JDBC API to bind arguments to query parameters, initiate execution of the query, scroll through the query result table, retrieve values from the result set, and so on. These are lowlevel data access tasks; as application developers, we’re more interested in the business problem that requires this data access. It isn’t clear that we should be concerning ourselves with such tedious, mechanical details. What we’d really like to be able to do is write code that saves and retrieves complex objects—the instances of our classes—to and from the database, relieving us of this low-level drudgery. Since the data access tasks are often so tedious, we have to ask: Are the relational data model and (especially) SQL the right choices for persistence in objectoriented applications? We answer this question immediately: Yes! There are many reasons why SQL databases dominate the computing industry. Relational database management systems are the only proven data management technology and are almost always a requirement in any Java project. However, for the last 15 years, developers have spoken of a paradigm mismatch. This mismatch explains why so much effort is expended on persistence-related concerns in every enterprise project. The paradigms referred to are object modeling and relational modeling, or perhaps object-oriented programming and SQL. Let’s begin our exploration of the mismatch problem by asking what persistence means in the context of object-oriented application development. First we’ll widen the simplistic definition of persistence stated at the beginning of this section to a broader, more mature understanding of what is involved in maintaining and using persistent data. 1.1.4 Persistence in object-oriented applications In an object-oriented application, persistence allows an object to outlive the process that created it. The state of the object may be stored to disk and an object with the same state re-created at some point in the future. This application isn’t limited to single objects—entire graphs of interconnected objects may be made persistent and later re-created in a new process. Most objects Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>