Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596517724/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
Hibernate is a lightweight object/relational mapping service for Java. What does that mean? Its a way to work easily and efficiently with information from a relational database in the form of natural Java objects. But that description doesnt come close to conveying how useful and exciting the technology is. Im not the only person who thinks so: Hibernate 2.1 won Software Development magazines 14th annual Jolt Award in the Libraries, Frameworks, and Components category. (And, with the hindsight afforded by the opportunity to write this updated and expanded version of Hibernate: A Developers Notebook, I can say I was truly thrilled that the first edition of this book, which covered Hibernate 2, was itself a Productivity Winner in the 15th annual Awards.)
So, whats great about Hibernate? All nontrivial applications (and even many trivial ones) need to store and use information, and these days this usually involves a relational database. Worlds apart from Java objects, databases often involve people with different skills and specializations. Bridging these two worlds has been important for a while, but it used to be quite complex and tedious.
Most people start out struggling to write a few SQL queries, embedding these awkwardly as strings within Java code, and working with Java database connectivity (JDBC) to run them and process the results. JDBC has evolved into a rich and flexible database communication library, which now provides ways to simplify and improve upon this approach, but there is still a fair degree of tedium involved. People who work with data a great deal need more powersome way of moving the queries out of the code and making them act more like well-behaved components in an object-oriented world.
Such capabilities had been part of my own (even more) lightweight object/relational layer for years. It began with a Java database connection and query pooling system written by my colleague Eric Knapp for the Lands End e-commerce site. Our pooler introduced the idea of external SQL templates that could be accessed by name and efficiently combined with runtime data to generate the actual database queries. Only later did it grow to include the ability to bind these templates directly to Java objects, by adding simple mapping directives to the templates.
Although far less powerful than a system like Hibernate, this approach proved valuable in many projects of different sizes and in widely differing environments. We continued to use it until the first version of this book, most recently in building IP telephony applications for Ciscos CallManager platform. However, we now use Hibernate for new projects, and once you work through this book, youll understand why. You will probably make the same decision yourself. Hibernate does a tremendous amount for you, and does it so easily that you can almost forget youre working with a database. Your objects are simply there when you need them. This is how technology should work.
You may wonder how Hibernate relates to Enterprise JavaBeans (EJBs). Is it a competing solution? When would you use one over the other? In fact, you can use both. Not every application needs the complexity of EJBs; many can simply use Hibernate directly to interact with a database. On the other hand, EJBs are sometimes indispensable for very complex three-tier application environments. In such cases, Hibernate may be used by an EJB Session bean to persist data, or it might be used to persist BMP entity beans.
In fact, even the , youll probably still want to take advantage of Hibernates extensions to JPA).
The development of Hibernate has clearly been a watershed event in the interaction between Java and relational databases. The Java world owes Gavin King and his intrepid cohorts a huge debt of thanks for making our lives easier. This book is intended to help you learn how take advantage of their achievement as quickly as possible.
How to Use This Book
This book started as part of OReilly's Developers Notebook series, a new approach to helping readers rapidly come up to speed with useful new technologies. Although it has since been expanded to touch on more of the related technologies that Hibernate users might want to explore, it is not intended to be a comprehensive reference manual for Hibernate. Instead, it reflects the authors own exploration of the system, from initial download and configuration through a series of projects that demonstrate how to accomplish a variety of practical goals.
By reading and following along with these examples, youll be able to get your own Hibernate environment set up quickly and start using it for realistic tasks right away. Its as if you can walk with us through terrain weve mapped out, while we point out useful landmarks and tricky pitfalls along the way.
Although we certainly include some background materials and explanations of how Hibernate works and why, this is always in the service of a focused task. Sometimes well refer you to the reference documentation or other online resources if youd like more depth about one of the underlying concepts or details about a related but different way to use Hibernate.
Once youre past the first few chapters, you dont need to read the rest in order; you can jump to topics that are particularly interesting or relevant to you. The examples do build on each other, but you can download the finished source code from the books web site (you may want to start with the previous chapters files and follow along, making changes yourself to implement the examples youre reading). You can always jump back to the earlier examples if they turn out to be interesting because of how they relate to what youve just learned.
Font Conventions
This book follows certain conventions for font usage. Understanding these conventions upfront will make it easier to use this book.