CONTENTS 7.5 Advanced query techniques 276 Dynamic queries 276.Collection filters 279 Subqueries 281.Native SOL queries 283 7.6 Optimizing object retrieval 286 Solving the n+1 selects problem 286Using iterate0 queries 289.Caching queries 290 7.7 Summary 292 Writing Hibernate applications 294 8.1 Designing layered applications 295 Using Hibernate in a servlet engine 296 Using Hibernate in an EIB container 311 8.2 Implementing application transactions 320 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 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
CONTENTS ix 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>
CONTENTS 9.5 XDoclet 372 9.6 Summary 376 appendix A:SOL fimndamentals 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
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 JavaTM 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 JavaT 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 foreword Relational databases are indisputably at the core of the modern enterprise. While modern programming languages, including JavaTM, 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 technologies, 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 JavaTM 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 knowledgeable both of the application and its data requirements, and of the SQL query language, 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. Hibernate’s developers designed it with modularity, pluggability, extensibility, and user customization in mind. As a result, in the few years since its initial release, Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
FOREWORD Hibernate has rapidly become one of the leading ORM technologies for enter prise developers-and deservedly so. This book provides a comprehensive overview of Hibernate.It covers how to use its type mapping capabilities and facilities for modeling associations and inheritance:how to retrieve objects efficiently using the Hibernate query lan guage;how to configure Hibernate for use in both managed and unmanaged environments:and how to 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/rela- tional mapping in enterprise computing today. LINDA DEMICHIEL Lead Architect,Enterprise JavaBeans Sun Microsystems
xii FOREWORD Hibernate has rapidly become one of the leading ORM technologies for enterprise developers—and deservedly so. This book provides a comprehensive overview of Hibernate. It covers how to use its type mapping capabilities and facilities for modeling associations and inheritance; how to retrieve objects efficiently using the Hibernate query language; how to configure Hibernate for use in both managed and unmanaged environments; and how to 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 Lead Architect, Enterprise JavaBeans Sun Microsystems Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
preface Just because it is possible to push twigs along the ground with one's nose does not necessarily mean that that is the best way to collect firewood. -Anthony Berglas Today,many software developers work with Enterprise Information Systems(EIS). This kind of application creates,manages,and stores structured information and shares this information between many users in multiple physical locations. The storage of EIS data involves massive usage of SQL-based database manage ment systems.Every company we've met during our careers uses at least one SQL database:most are completely dependent on relational database technology at the core of their business. In the past five years,broad adoption of the Java programming language has brought about the ascendancy of the object-oriented paradigm for software devel opment.Developers are now sold on the benefits of object orientation.However. the vast majority of businesses are also tied to long-term investments in expensive relational database systems.Not only are particular vendor products entrenched. but existing legacy data must be made available to(and via)the shiny new object oriented web applications. However,the tabular representation of data in a relational system is fundamen- tally different than the networks of objects used in object-oriented Java applica- tions.This difference has led to the so-called object/relational paradigm mismatch Traditionally,the importance and cost of this mismatch have been underesti- mated,and tools for solving the mismatch have been insufficient.Meanwhile.Java developers blame relational technology for the mismatch;data professionals blame object technology. xi
xiii preface Just because it is possible to push twigs along the ground with one’s nose does not necessarily mean that that is the best way to collect firewood. —Anthony Berglas Today, many software developers work with Enterprise Information Systems (EIS). This kind of application creates, manages, and stores structured information and shares this information between many users in multiple physical locations. The storage of EIS data involves massive usage of SQL-based database management systems. Every company we’ve met during our careers uses at least one SQL database; most are completely dependent on relational database technology at the core of their business. In the past five years, broad adoption of the Java programming language has brought about the ascendancy of the object-oriented paradigm for software development. Developers are now sold on the benefits of object orientation. However, the vast majority of businesses are also tied to long-term investments in expensive relational database systems. Not only are particular vendor products entrenched, but existing legacy data must be made available to (and via) the shiny new objectoriented web applications. However, the tabular representation of data in a relational system is fundamentally different than the networks of objects used in object-oriented Java applications. This difference has led to the so-called object/relational paradigm mismatch. Traditionally, the importance and cost of this mismatch have been underestimated, and tools for solving the mismatch have been insufficient. Meanwhile, Java developers blame relational technology for the mismatch; data professionals blame object technology. Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>