1、Understanding object/relational persistence The approach to managing persistent data has been a key design decision in every software project weve worked on. Given that persistent data isnt a new or unusual requirement for Java applications, youd expect to be able to make a simple choice among simil
2、ar, 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
3、scope and overall approach. Unfortunately, this isnt 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 dont even agree on the scope of the pr
4、oblem. 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, re
5、ad, 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 re
6、cently 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 applications interacti
7、on 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 arent required to follow many Hibernate-specific rules and design patterns when writing your business logic and persistent classes; th
8、us, Hibernate integrates smoothly with most new and existing applications and doesnt require disruptive changes to the rest of the application. This article is about Hibernate. Well cover basic and advanced features and describe some recommended ways to develop new applications using Hibernate.Often
9、, these recommendations wont be specific to Hibernatesometimes 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 persis
10、tence and object/relational mapping. This chapter explains why tools like Hibernate are needed. 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
11、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 spen
12、d on the persistence-related code of our applications. After we look at alternative tools and persistence mechanisms, youll 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 wh
13、en picking a persistence solution for your own project. What is persistence? Almost all applications require persistent data. Persistence is one of the fundamental concepts in application development. If an information system didnt preserve data entered by users when the host machine was powered off
14、, the system would be of little practical use. When we talk about persistence in Java, were 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 discuss
15、ion of persistence and how its implemented in object-oriented applications. 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 sufficie
16、nt 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 theyre an incredibly flexible and robust approach to data management. A relational database management system isnt specific to Ja
17、va, and a relational database isnt 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). Relation
18、al 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
19、todays relational database products SQL database management systems or, when were talking about particular systems, SQL databases. Understanding SQL To use Hibernate effectively, a solid understanding of the relational model and SQL is a prerequisite. Youll need to use your knowledge of SQL to tune
20、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, effi
21、cient management of persistent data.Lets 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 languag
22、e (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, y
23、ou 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 fr
24、om 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 weve mentioned are new or unclear.SQL knowledge is mandatory for sound Java da
25、tabase application development.If you need more material, get a copy of the excellent book SQL Tuning by Dan Tow Also read An Introduction to Database Systems Date 2004 for the theory, concepts, and ideals of (relational) database systems. Although the relational database is one part of ORM, the oth
26、er part, of course, consists of the objects in your Java application that need to be persisted to the database using SQL. 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
27、 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,
28、 and so on. These are lowlevel data access tasks; as application developers, were more interested in the business problem that requires this data access. It isnt clear that we should be concerning ourselves with such tedious, mechanical details.What wed really like to be able to do is write code tha
29、t saves and retrieves complex objectsthe instances of our classesto 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
30、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 yea
31、rs, 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. Lets begin our e
32、xploration of the mismatch problem by asking what persistence means in the context of object-oriented application development. First well 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. 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