Hibernate in Action pdf

318 439 0
Hibernate in Action pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Hibernate in Action Hibernate in Action CHRISTIAN BAUER GAVIN KING MANNING Greenwich (74° w. long.) Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: manning@manning.com ©2005 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. . Manning Publications Co. Copyeditor: Tiffany Taylor 209 Bruce Park Avenue Typesetter: Dottie Marsico Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1932394-15-X Printed in the United States of America 12 34567 8910– VHG–07 06 05 04 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> contents foreword xi about Hibernate3 and EJB 3 xx author online xxi about the title and cover xxii preface xiii acknowledgments xv about this book xvi 1 Understanding object/relational persistence 1 1.1 What is persistence? 3 Relational databases 3 ■ Understanding SQL 4 ■ Using SQL in Java 5 ■ Persistence in object-oriented applications 5 1.2 The paradigm mismatch 7 The problem of granularity 9 ■ The problem of subtypes 10 The problem of identity 11 ■ Problems relating to associations 13 The problem of object graph navigation 14 ■ The cost of the mismatch 15 1.3 Persistence layers and alternatives 16 Layered architecture 17 ■ Hand-coding a persistence layer with SQL/JDBC 18 ■ Using serialization 19 ■ Considering EJB entity beans 20 ■ Object-oriented database systems 21 Other options 22 1.4 Object/relational mapping 22 What is ORM? 23 ■ Generic ORM problems 25 Why ORM? 26 1.5 Summary 29 v Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> vi CONTENTS 2 Introducing and integrating Hibernate 30 2.1 “Hello World” with Hibernate 31 2.2 Understanding the architecture 36 The core interfaces 38 ■ Callback interfaces 40 Types 40 ■ Extension interfaces 41 2.3 Basic configuration 41 Creating a SessionFactory 42 ■ Configuration in non-managed environments 45 ■ Configuration in managed environments 48 2.4 Advanced configuration settings 51 Using XML-based configuration 51 SessionFactory 53 ■ Logging 54 Extensions (JMX) 55 2.5 Summary 58 3 Mapping persistent classes 59 ■ JNDI-bound ■ Java Management 3.1 The CaveatEmptor application 60 Analyzing the business domain 61 The CaveatEmptor domain model 61 3.2 Implementing the domain model 64 Addressing leakage of concerns 64 ■ Transparent and automated persistence 65 ■ Writing POJOs 67 Implementing POJO associations 69 ■ Adding logic to accessor methods 73 3.3 Defining the mapping metadata 75 Metadata in XML 75 ■ Basic property and class mappings 78 ■ Attribute-oriented programming 84 Manipulating metadata at runtime 86 3.4 Understanding object identity 87 Identity versus equality 87 ■ Database identity with Hibernate 88 ■ Choosing primary keys 90 3.5 Fine-grained object models 92 Entity and value types 93 ■ Using components 93 3.6 Mapping class inheritance 97 Table per concrete class 97 ■ Table per class hierarchy 99 Table per subclass 101 ■ Choosing a strategy 104 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> vii CONTENTS 3.7 Introducing associations 105 Managed associations? 106 ■ Multiplicity 106 The simplest possible association 107 ■ Making the association bidirectional 108 ■ A parent/child relationship 111 3.8 Summary 112 4 Working with persistent objects 114 4.1 The persistence lifecycle 115 Transient objects 116 ■ Persistent objects 117 ■ Detached objects 118 ■ The scope of object identity 119 ■ Outside the identity scope 121 ■ Implementing equals() and hashCode() 122 4.2 The persistence manager 126 Making an object persistent 126 ■ Updating the persistent state of a detached instance 127 ■ Retrieving a persistent object 129 Updating a persistent object 129 ■ Making a persistent object transient 129 ■ Making a detached object transient 130 4.3 Using transitive persistence in Hibernate 131 Persistence by reachability 131 ■ Cascading persistence with Hibernate 133 ■ Managing auction categories 134 Distinguishing between transient and detached instances 138 4.4 Retrieving objects 139 Retrieving objects by identifier 140 ■ Introducing HQL 141 Query by criteria 142 ■ Query by example 143 ■ Fetching strategies 143 ■ Selecting a fetching strategy in mappings 146 Tuning object retrieval 151 4.5 Summary 152 5 Transactions, concurrency, and caching 154 5.1 Transactions, concurrency, and caching 154 5.2 Understanding database transactions 156 JDBC and JTA transactions 157 ■ The Hibernate Transaction API 158 ■ Flushing the Session 160 ■ Understanding isolation levels 161 ■ Choosing an isolation level 163 ■ Setting an isolation level 165 ■ Using pessimistic locking 165 5.3 Working with application transactions 168 Using managed versioning 169 ■ Granularity of a Session 172 ■ Other ways to implement optimistic locking 174 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> viii CONTENTS 5.4 Caching theory and practice 175 Caching strategies and scopes 176 ■ The Hibernate cache architecture 179 ■ Caching in practice 185 5.5 Summary 194 6 Advanced mapping concepts 195 6.1 Understanding the Hibernate type system 196 Built-in mapping types 198 ■ Using mapping types 200 6.2 Mapping collections of value types 211 Sets, bags, lists, and maps 211 6.3 Mapping entity associations 220 One-to-one associations 220 ■ Many-to-many associations 225 6.4 Mapping polymorphic associations 234 Polymorphic many-to-one associations 234 ■ Polymorphic collections 236 ■ Polymorphic associations and table-per- concrete-class 237 6.5 Summary 239 7 Retrieving objects efficiently 241 7.1 Executing queries 243 The query interfaces 243 ■ Binding parameters 245 Using named queries 249 7.2 Basic queries for objects 250 The simplest query 250 ■ Using aliases 251 ■ Polymorphic queries 251 ■ Restriction 252 ■ Comparison operators 253 String matching 255 ■ Logical operators 256 ■ Ordering query results 257 7.3 Joining associations 258 Hibernate join options 259 ■ Fetching associations 260 265 268 Using aliases with joins 262 ■ Using implicit joins Theta-style joins 267 ■ Comparing identifiers 7.4 Writing report queries 269 Projection 270 ■ Using aggregation 272 ■ Grouping 273 Restricting groups with having 274 ■ Improving performance with report queries 275 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> ix CONTENTS 7.5 Advanced query techniques 276 Dynamic queries 276 ■ Collection filters 279 Subqueries 281 ■ Native SQL queries 283 7.6 Optimizing object retrieval 286 Solving the n+1 selects problem 286 ■ Using iterate() queries 289 ■ Caching queries 290 7.7 Summary 292 8 Writing Hibernate applications 294 8.1 Designing layered applications 295 Using Hibernate in a servlet engine 296 Using Hibernate in an EJB container 311 8.2 Implementing application transactions 320 Approving a new auction 321 ■ Doing it the hard way 322 Using detached persistent objects 324 ■ Using a long session 325 Choosing an approach to application transactions 329 8.3 Handling special kinds of data 330 Legacy schemas and composite keys 330 ■ Audit logging 340 8.4 Summary 347 9 Using the toolset 348 9.1 Development processes 349 Top down 350 ■ Bottom up 350 ■ Middle out (metadata oriented) 350 ■ Meet in the middle 350 Roundtripping 351 9.2 Automatic schema generation 351 Preparing the mapping metadata 352 ■ Creating the schema 355 ■ Updating the schema 357 9.3 Generating POJO code 358 Adding meta-attributes 358 ■ Generating finders 360 Configuring hbm2java 362 ■ Running hbm2java 363 9.4 Existing schemas and Middlegen 364 Starting Middlegen 364 ■ Restricting tables and relationships 366 ■ Customizing the metadata generation 368 Generating hbm2java and XDoclet metadata 370 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> x CONTENTS 9.5 XDoclet 372 Setting value type attributes 372 ■ Mapping entity associations 374 ■ Running XDoclet 375 9.6 Summary 376 appendix A: SQL fundamentals 378 appendix B: ORM implementation strategies 382 B.1 Properties or fields? 383 B.2 Dirty-checking strategies 384 appendix C: Back in the real world 388 C.1 The strange copy 389 C.2 The more the better 390 C.3 We don’t need primary keys 390 C.4 Time isn’t linear 391 C.5 Dynamically unsafe 391 C.6 To synchronize or not? 392 C.7 Really fat client 393 C.8 Resuming Hibernate 394 references 395 index 397 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es> foreword Relational databases are indisputably at the core of the modern enterprise. While modern programming languages, including Java TM , provide an intuitive, object-oriented view of application-level business entities, the enterprise data underlying these entities is heavily relational in nature. Further, the main strength of the relational model—over earlier navigational models as well as over later OODB models—is that by design it is intrinsically agnostic to the programmatic manipulation and application-level view of the data that it serves up. Many attempts have been made to bridge relational and object-oriented tech- nologies, or to replace one with the other, but the gap between the two is one of the hard facts of enterprise computing today. It is this challenge—to provide a bridge between relational data and Java TM objects—that Hibernate takes on through its object/relational mapping ( ORM) approach. Hibernate meets this challenge in a very pragmatic, direct, and realistic way. As Christian Bauer and Gavin King demonstrate in this book, the effective use of ORM technology in all but the simplest of enterprise environments requires understanding and configuring how the mediation between relational data and objects is performed. This demands that the developer be aware and knowledge- able both of the application and its data requirements, and of the SQL query lan- guage, relational storage structures, and the potential for optimization that relational technology offers. Not only does Hibernate provide a full-function solution that meets these requirements head on, it is also a flexible and configurable architecture. Hiber- nate’s developers designed it with modularity, pluggability, extensibility, and user customization in mind. As a result, in the few years since its initial release, xi [...]... use its tools In addition, throughout the book the authors provide insight into the underlying issues of ORM and into the design choices behind Hibernate These insights give the reader a deep understanding of the effective use of ORM as an enterprise technology Hibernate in Action is the definitive guide to using Hibernate and to object/relational mapping in enterprise computing today LINDA DEMICHIEL... you try asking the authors some challenging questions lest their interest stray! xxi Licensed to Jose Carlos Romero Figueroa about the title and cover By combining introductions, overviews, and how-to examples, Manning’s In Action books are designed to help learning and remembering According to research in cognitive science, the things people remember are things they... became overwhelming, the Hibernate team concluded that the future success of the project (and Gavin’s continued sanity) demanded professional developers dedicated full-time to Hibernate Hibernate joined jboss.org in late 2003 and now has a commercial aspect; you can purchase commercial support and training from JBoss Inc But commercial training shouldn’t be the only way to learn about Hibernate It’s... source code listings, highlighting important concepts In some cases, numbered bullets link to explanations that fol­ low the listing Hibernate is an open source project released under the Lesser GNU Public License Directions for downloading Hibernate, in source or binary form, are available from the Hibernate web site: www .hibernate. org/ The source code for all CaveatEmptor examples in this book is... HTML, and XML can all be verbose In many cases, the original source code (available online) has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book In rare cases, even this was not enough, and listings include line-continuation markers Addi­ tionally, comments in the source code have been removed from the listings Licensed to Jose Carlos... for the Hibernate web site and documentation Christian is interested in rela­ tional database systems and sound data management in Java applications He works as a developer and consultant for JBoss Inc and lives in Frankfurt, Germany Gavin King is the founder of the Hibernate project and lead developer He is an enthusiastic proponent of agile development and open source software Gavin is helping integrate... and APIs from Hibernate in its redesign of entity beans At the time of writ­ ing, only an early draft of the new EJB specification was available; hence we don’t discuss it in this book However, after reading Hibernate in Action, you’ll know all the fundamentals that will let you quickly understand entity beans in EJB 3 For more up-to-date information, see the Hibernate road map: www .hibernate. org/About/RoadMap... teaches you the Hibernate interfaces for read and save operations; we also show you how transitive persistence (persistence by reachability) works in Hibernate This chapter is focused on loading and storing objects in the most effi­ cient way Chapter 5 discusses concurrent data access, with database and long-running application transactions We introduce the concepts of locking and versioning of data We... they discover during self-motivated exploration Although no one at Manning is a cognitive scientist, we are convinced that for learning to become permanent it must pass through stages of exploration, play, and, interestingly, re-telling of what is being learned People understand and remember new things, which is to say they master them, only after actively explor­ ing them Humans learn in action An essential... 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 . Using Hibernate in a servlet engine 296 Using Hibernate in an EJB container 311 8.2 Implementing application transactions 320 Approving a new auction 321 ■ Doing it the hard way 322 Using. 258 Hibernate join options 259 ■ Fetching associations 260 265 268 Using aliases with joins 262 ■ Using implicit joins Theta-style joins 267 ■ Comparing identifiers 7.4 Writing report. understanding of the effective use of ORM as an enterprise technology. Hibernate in Action is the definitive guide to using Hibernate and to object/rela- tional mapping in enterprise computing today.

Ngày đăng: 27/06/2014, 11:20

Từ khóa liên quan

Mục lục

  • contents

  • foreword

  • preface

  • acknowledgments

  • about this book

    • Roadmap

    • Who should read this book?

    • Code conventions and downloads

    • About the authors

    • about Hibernate3 and EJB 3

    • author online

    • about the title and cover

      • About the cover illustration

      • Understanding object/relational persistence

        • 1.1 What is persistence?

          • 1.1.1 Relational databases

          • 1.1.2 Understanding SQL

          • 1.1.3 Using SQL in Java

          • 1.1.4 Persistence in object-oriented applications

          • 1.2 The paradigm mismatch

            • 1.2.1 The problem of granularity

            • 1.2.2 The problem of subtypes

            • 1.2.3 The problem of identity

            • 1.2.4 Problems relating to associations

            • 1.2.5 The problem of object graph navigation

Tài liệu cùng người dùng

Tài liệu liên quan