《企业网站管理中英文翻译资料.doc》由会员分享,可在线阅读,更多相关《企业网站管理中英文翻译资料.doc(9页珍藏版)》请在三一文库上搜索。
1、Understanding object/relational persistenceThe 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 simila
2、r, 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 s
3、cope 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 pro
4、blem. 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, rea
5、d, 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 rec
6、ently 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 interactio
7、n 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; thu
8、s, 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 persiste
10、nce 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 bui
11、lt 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 thetime we have to spend on
12、 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 when p
13、icking 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, the
14、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 discussion of
15、 persistence and how its implemented in object-oriented applications.Relational databasesYou, 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 reaso
16、n 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 Java, and a
17、 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). Relational techno
18、logy 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 todays re
19、lational database products SQL database management systems or, when were talking about particular systems, SQL databases.Understanding SQLTo 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 the perform
20、ance 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 manag
21、ement 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 language (DML). Wi
22、th 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
23、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
24、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 database appl
25、ication development.If you need more material, get a copy of the excellent book SQL Tuning by Dan TowAlso 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 other part, of
26、course, consists of the objects in your Java application that need to be persisted to the database using SQL.Using SQL in JavaWhen 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 mi
27、ght 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. Th
28、ese 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 that saves and re
29、trieves 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 applications?
30、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
31、 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 exploration of
32、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 pers
33、istent data.Persistence in object-oriented applicationsIn 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 isnt
34、limited to single objectsentire graphs of interconnected objects may be made persistent and later re-created in a new process. Most objects arent persistent; a transient object has a limited lifetime that is bounded by the life of the process that instantiated it. Almost all Java applications contai
35、n a mix of persistent and transient objects; hence we need a subsystem that manages our persistent data.Modern relational databases provide a structured representation of persistent data, enabling sorting, searching, and aggregation of data. Database management systems are responsible for managing c
36、oncurrency and data integrity; theyreresponsible for sharing data between multiple users and multiple applications. A database management system also provides data-level security. When we discuss persistence in this book, were thinking of all these things: Storage, organization, and retrieval of str
37、uctured data Concurrency and data integrity Data sharingIn particular, were thinking of these problems in the context of an object-oriented application that uses a domain model.An application with a domain model doesnt work directly with the tabular representation of the business entities; the appli
38、cation has its own, object-oriented model of the business entities. If the database has ITEM and BID tables, the Java application defines Item and Bid classes.Then, instead of directly working with the rows and columns of an SQL result set, the business logic interacts with this object-oriented doma
39、in model and its runtime realization as a graph of interconnected objects. The business logic is never executed in the database (as an SQL stored procedure), its implemented in Java. This allows business logic to make use of sophisticated object-oriented concepts such as inheritance and polymorphism
40、. For example, we could use wellknown design patterns such as Strategy, Mediator, and Composite GOF 1995, all of which depend on polymorphic method calls. Now a caveat: Not all Java applications are designed this way, nor should they be. JDBC RowSet (Sun JCP, JSR 114) makes CRUD operations even easi
41、er. Working with a tabular , the domain model helps to improve code reuse and maintainability significantly. We focus on applications with a domain model in this book, since Hibernate and ORM in general are most relevant to this kind of application. If we consider SQL and relational databases again,
42、 we finally observe the mismatch between the two paradigms. SQL operations such as projection and join always result in a tabular representation of the resulting data. This is quite different than the graph of interconnected objects used to execute the business logic in a Java application! These are
43、 fundamentally different models, not just different ways of visualizing the same model. With this realization, we can begin to see the problemssome well understood and some less well understoodthat must be solved by an application that combines both data representations: an object-oriented domain mo
44、del and a persistentrelational model. Lets take a closer look.理解对象-关系持续性我们工作的每个软件项目工程中,管理持续性数据的方法已经成为一项关键的设计决定。对于Java应用,持续性数据并不是一个新的或不寻常的需求,你也许曾经期望能够在许多相似的,已被很好构建的持续性解决方案中简单地进行选择。考虑一下Web应用框架(Jakarta Struts 对 WebWork),GUI组件框架(Swing 对 SWT),或模版工具(JSP 对 Velocity)。每一种相互竞争的解决方案都有其优缺点,但它们至少都共享了相同的范围与总体的方法。
45、不幸的是,这还不是持续性技术的情形,对持续性技术相同的问题有许多不同的混乱的解决方案。在过去的几年里,持续性已经成为Java社区里一个争论的热点话题。对这个问题的范围许多开发者的意见甚至还不一致。持续性还是一个问题吗?它早已被关系技术与其扩展例如存储过程解决了。或者它是一个更一般的问题,必须使用特殊的Java组件模型例如EJB实体Bean来处理?甚至SQL和JDBC中最基本的CRUD(create, read, update, delete)操作也需要进行手工编码,还是让这些工作自动化?如果每一种数据库管理系统都有它自己的方言,我们如何达到可移植性?我们应该完全放弃SQL并采用一种新的数据库技
46、术,例如面向对象数据库系统吗?争论仍在继续,但是最近一种称作对象-关系映射(ORM)的解决方案逐渐地被接受。Hibernate就是这样一种开源的ORM实现。Hibernate是一个雄心勃勃的项目,它的目标是成为Java中管理持续性数据问题的一种完整的解决方案。它协调应用与关系数据库的交互,让开发者解放出来专注于手中的业务问题。Hibernate是一种非强迫性的解决方案。我们的意思是指在写业务逻辑与持续性类时,你不会被要求遵循许多Hibernate特定的规则和设计模式。这样,Hibernate就可以与大多数新的和现有的应用平稳地集成,而不需要对应用的其余部分作破坏性的改动。本篇文章是关于Hibe
47、rnate的。我们包含了基本与高级的特征,并且描述了许多使用Hibernate开发新应用时的推荐方式。通常这些推荐并不特定于Hibernate有时它们可能是我们关于使用持续性数据工作时处理事情的最佳方式的一些想法,只不过在Hibernate的环境中进行了介绍。然而,在我们可以开始使用Hibernate之前,你需要理解对象持续性和对象-关系映射的核心问题。本章解释了为什么像Hibernate这样的工具是必需的。首先,我们定义了在面向对象的应用环境中持续性数据的管理,并且讨论了SQL,JDBC和Java的关系,Hibernate就是在这些基础的技术与标准之上构建的。然后我们讨论了所谓的对象-关系范
48、例不匹配的问题和使用关系数据库进行面向对象的软件开发中所遇到的一些一般性的问题。随着这个问题列表的增长,我们需要一些工具与模式来最小化我们用在与持续性有关的代码上的时间就变得很明显了。在我们查看了可选的工具和持续性机制后,你会发现ORM在许多情况下可能是最好的解决方案。我们关于ORM的优缺点的讨论给了你一个完整的背景,在你为自己的项目选择持续性解决方案时可以作出最好的决定。什么是持续性?几乎所有的应用都需要持续性数据。持续性在应用开发中是一个基本的概念。如果当主机停电时一个信息系统没有保存用户输入的数据,这样的系统几乎没有实际的用途。当我们讨论Java中的持续性时,我们通常是指使用SQL存储在
49、关系数据库中的数据。我们从简单地查看一下这项技术和我们如何在Java中使用它开始。具有了这些知识之后,我们继续关于持续性的讨论以及如何在面向对象的应用中实现它。关系数据库你,像许多其他的开发者,很可能在使用一个关系数据库进行工作。实际上,我们中的大多数每天都在使用关系数据库。关系技术是一个已知数。仅此一点就是许多组织选择它的一个充分的理由。但仅仅这样说就有点欠考虑了。关系数据库如此不易改变不是因为偶然的原因而是因为它们那令人难以置信的灵活与健壮的数据管理方法。关系数据库管理系统既不特定于Java,也不特定于特殊的应用。关系技术提供了一种在不同的应用或者相同应用的不同技术(例如事务工具与报表工具)之间共享数据的方式。关系技术是多种不同的系统与技术平台之间的一个共同特征。因此,关系数据模型通常是业务实体共同的企业级表示。关系数据库管理系统具有基于SQL的应用编程接口,因此我们称今天的关系数据库产品为SQL数据库管理系统,或者当我们讨论特定系统时,称之为SQL数据库。理解SQL为了有效地使用Hibernate,对关系模型和SQL扎实
链接地址:https://www.31doc.com/p-3260950.html