About this book

Hibernate is a solid, productive Object Relational Mapping (ORM) tool that lets developers think and work with objects rather than tables and columns. It has grown over the years, been used by many developers, and has gone through three major versions. This book's goal is to make you productive with Hibernate.

Hibernate Quickly is a gradual introduction to the features of Hibernate, covering the latest version, Hibernate 3. Each chapter introduces a series of concepts that form a foundation for the next chapter, but should illustrate those concepts completely. We set out to write the book we would have liked to have when we first learned Hibernate. We both think that studying good code examples is one of the best ways to learn, so we included as many as we could. We also wanted to make our own reference, a book we could have on our desk and turn to when we forgot just how that one mapping needed to be written.

Developers don't work with Hibernate in a vacuum. In addition to standard Java, developers often use Hibernate with a host of other third-party (often open source) tools and libraries, including J2EE (web applications); build tools like Ant; unit-testing frameworks like JUnit; and frameworks like XDoclet, Struts, WebWork, Tapestry, and Spring. This book shows how Hibernate fits into your development projects by demonstrating how these third party tools can be integrated with Hibernate. Because this book is about Hibernate, and we didn't want it to be 1,000 pages long or weigh as much as a battleship, we assume that developers are partially familiar with the third-party libraries with which they want to integrate Hibernate. We provide some introduction, so you should be able to follow along if you've used, say, Tapestry; but you should consult Manning's In Action series for more details about those tools.

Roadmap

Hibernate Quickly is logically divided into two parts. The first half of the book introduces the core concepts and features of Hibernate. The second half puts Hibernate into context by showing you how to integrate it with a number of open source tools and frameworks.

Chapter 1 is both a justification and an introduction. It covers the reasons why Hibernate is useful, and it compares and contrasts Hibernate with JDBC. It also covers the basics of what object relational mapping is and provides an introduction to how Hibernate's particular brand of persistence works.

Chapter 2 is the project kickoff. It covers setting up a Hibernate project and using Ant, an open source Java build tool. It shows you where to find both Ant and Hibernate and how to organize your project. We also discuss setting up and integrating a database, MySQL. By the end of this chapter, you should have a solid foundation for your project to build on in the subsequent chapters.

Chapter 3 is about the core concepts of Hibernate. It covers mapping files, configuration, and the essential classes developers use to persist and find objects from the database. Finally, this chapter touches on a few more advanced topics like inheritance, caching, and transactions.

Chapter 4 discusses relationships. Specifically, we cover in detail two of the most common relationships between persistent objects: many-to-one and components. This chapter also explains how to generate your database from Hibernate mappings using the SchemaExport tool.

Chapter 5 covers collections and custom types. Hibernate allows you to use basic java.util collections classes to express both one-to-many and many-to-many relationships between entities, and we show how to map and use them here. In addition, we demonstrate both how and why you can use Hibernate's custom types, which let you define new datatypes that can map to database columns.

Chapter 6 discusses finding objects. Hibernate uses a SQL-like query language that allows you to express queries using classes and properties rather than tables and columns. This chapter covers Hibernate Query Language's (HQL) syntax and usage in depth, including parameters, joins, from/select clauses, and projection. So that you can test your HQL, the chapter also touches on Hibern8IDE, a tool that lets you rapidly test and try your queries.

Chapter 7 is about patterns and architecture. We show you how to better organize your project with a few patterns like the Data Access Object (DAO) and Layer Super types. We also explain how to introduce a popular application framework, Spring, into your project. Spring integrates extremely well with Hibernate; it streamlines the DAO pattern along with some of the productive boosting features.

Chapter 8 discusses "webifying" your Hibernate application. We cover the basics of the Model View Controller pattern; and we build our sample application, the Event Calendar, using three open source web frameworks. The same application is written three times using a similar core architecture but integrated with Struts, WebWork, and Tapestry. Our intent is to show the general principles you need to consider when writing Hibernate applications for the Web.

Chapter 9 covers code generation with XDoclet. Until JDK 1.5/Java 5.0 becomes more widely accepted, Hibernate developers can either hand-write their mapping files or, better yet, use XDoclet to generate them. This chapter shows you how to do the latter. We go in depth to show you how to set up, configure, and generate mapping files for single objects, many-to-one, components, and collections. In addition, we explain how to generate your configuration files, hibernate.cfg.xml, using XDoclet.

Chapter 10 is about testing. It shows you how to use two tools, JUnit and DBUnit, to verify that your Hibernate application works as expected. We cover the general principles of unit testing and how to apply them to testing a database.

Chapter 11 discusses Hibernate 3. It's a brief guide for those who are familiar with Hibernate 2 and want to know what's new. This chapter covers the important new features, including filters, mapping file improvements, dynamic classes, and the new persistent event model.

The appendix is the reference we wanted for ourselves. It's a complete reference guide to all the common relationships that Hibernate allows. For each relationship, the appendix shows an object model, table models, Java classes (with XDoclet markup), and the resulting mapping file.

Who should read this book?

In short, Java developers who work with databases. More specifically, we aimed this book at two main groups of developers:

We assume you're familiar with basic object-oriented programming techniques as well as the Java language. We discuss a lot of third-party tools, like Ant and XDoclet, but no in-depth familiarity with them is needed. Because Hibernate builds on JDBC and uses databases, it's helpful if you're familiar with SQL and how to use it to work with databases using Java.

Code

The code for this project is available at this book's website, www.manning.com/books/peak.

Much of the source code shown early in the book consists of fragments designed to illustrate the text. When a complete segment of code is given, it's shown as a numbered listing; code annotations accompany some listings. When we present source code, we sometimes use a bold font to draw attention to specific elements.

In the text, a monospaced font is used to denote code (JSP, Java, and HTML) as well as Java methods, JSP tag names, and other source code identifiers:

A reference to a method in the text generally doesn't include the signature, because there may be more than one form of the method call.

A reference to a JSP tag includes the braces and default prefix but not the list of properties the tag accepts (<c:out>).

A reference to an XML element in the text includes the braces but not the properties or closing tag (<class>).

Author Online

Purchase of Hibernate Quickly includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/peak. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning's commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking them some challenging questions lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher's website as long as the book is in print.

About the authors

Patrick Peak is the chief technology officer of BrowserMedia, a Java/J2EE web development/design firm in Bethesda, MD. His focus is on using open source frameworks/tools as a competitive advantage for rapid custom software development. He has been using Hibernate in numerous production applications for almost two years. He runs a Java/Technology weblog at www.patrickpeak.com.

Nick Heudecker is the president and founder of System Mobile, a software consulting firm headquartered in Chicago, IL. He has more than nine years of commercial development experience, and he has developed software products and solutions for multiple Fortune 500 clients as well as media, lobbying, and government organizations.