Joseph B. Ottinger,
Jeff Linwood and
Dave Minter
4th ed.
Joseph B. Ottinger
Youngsville, North Carolina, USA
Jeff Linwood
Austin, Texas, USA
Dave Minter
London, UK
Any source code or other supplementary materials referenced by the author in this text are available to readers at www.apress.com . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter.
ISBN 978-1-4842-2318-5
e-ISBN 978-1-4842-2319-2
DOI 10.1007/978-1-4842-2319-2
Library of Congress Control Number: 2016959194
© Joseph B. Ottinger, Jeff Linwood and Dave Minter 2016
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made.
Printed on acid-free paper
Dedicated to recursive dedications found around the globe. Again. And, still, to my beloved wife.
Hibernate is an amazing piece of software. With a little experience and the power of annotations, you can build a complex, database-backed system with disturbing ease. Once you have built a system using Hibernate, you will never want to go back to the traditional approaches.
While Hibernate is incredibly powerful, it presents a steep learning curve when you first encounter it—steep learning curves are actually a good thing because they impart profound insight once you have scaled them. Yet gaining that insight takes some perseverance and assistance.
Our aim in this book is to help you scale that learning curve by presenting you with the minimal requirements of a discrete Hibernate application, explaining the basis of those requirements, and walking you through an example application that is built using them. We then provide additional material to be digested once the fundamentals are firmly understood. Throughout, we provide examples rather than relying on pure discourse. We hope that you will continue to find this book useful as a reference text long after you have become an expert on the subject.
This book assumes a good understanding of Java fundamentals and some slight familiarity with database programming using the Java Database Connectivity (JDBC) API. We don’t expect you to know anything about Hibernate—but if you buy this book, it will probably be because you have had some exposure to the painful process of building a large database-based system.
All of our examples use open—source software—primarily the Hibernate API itself—so you will not need to purchase any software to get started with Hibernate development. This book is not an academic text. Our focus is, instead, on providing extensive examples and taking a pragmatic approach to the technology that it covers.
To true newcomers to the Hibernate API, we recommend that you read at least the first three chapters in order before diving into the juicy subjects of later chapters. Very experienced developers or those with experience with tools similar to Hibernate will want to skim the latter half of the book for interesting chapters.
This book is informally divided into three parts. Chapters 1 through 8 describe the fundamentals of Hibernate, including configuration, the creation of mapping files, and the basic APIs. Chapters 9 through 11 describe the use of queries, criteria, and filters to access the persistent information in more sophisticated ways. Chapter 12 addresses the use of Hibernate to talk to nonrelational data stores, providing an easy "on ramp" to NoSQL.
Finally, the appendixes discuss features that you will use less often or that are peripheral to the core Hibernate functionality. The following list describes more fully the contents of each chapter:
Chapter 1 outlines the purpose of persistence tools and presents excerpts from a simple example application to show how Hibernate can be applied. It also introduces core terminology and concepts.
Chapter 2 discusses the fundamentals of configuring a Hibernate application. It presents the basic architecture of Hibernate and discusses how a Hibernate application is integrated into an application.
Chapter 3 presents an example application, walking you through the complete process of creating and running the application. It then looks at a slightly more complex example and introduces the notion of generating the database schema directly from Hibernate annotations.
Chapter 4 covers the Hibernate lifecycle in depth. It discusses the lifecycle in the context of the methods available on the core interfaces. It also introduces key terminology and discusses the need for cascading and lazy loading.
Chapter 5 explains why mapping information must be retained by Hibernate and demonstrates the various types of associations that can be represented by a relational database. It briefly discusses the other information that can be maintained within a Hibernate mapping.
Chapter 6 explains how Hibernate lets you use the annotations to represent mapping information. It provides detailed examples for the most important annotations, and discusses the distinctions between the standard JPA 2 annotations and the proprietary Hibernate ones.
Chapter 7 explains some of the uses of the Java Persistence API (as opposed to the Hibernate-native API), as well as the lifecycle and validation of persisted objects.
Chapter 8 revisits the Hibernate Session object in detail, explaining the various methods that it provides. The chapter also discusses the use of transactions, locking, and caching, as well as how to use Hibernate in a multithreaded environment.
Chapter 9 discusses how Hibernate can be used to make sophisticated queries against the underlying relational database using the built-in Hibernate Query Language (HQL).
Chapter 10 introduces the Criteria API, which is a programmatic analog of the query language discussed in Chapter 9 .
Chapter 11 discusses how the Filter API can be used to restrict the results of the queries introduced in Chapters 9 and 10 .
Chapter 12 introduces Hibernate OGM, which maps objects to non-relational data stores like Infinispan and Mongodb, among others. It shows some of the uses of Hibernate Search to provide a common search facility for NoSQL, as well as offering full text query support.
Chapter 13 covers Hibernate Envers, which is a library that provides versioned data for entities stored through hibernate.
The source code for this book is available to readers from www.apress.com , in the Source Code/Download section. Please feel free to visit the Apress web site and download all the code from there.
We welcome feedback from our readers. If you have any queries or suggestions about this book, or technical questions about Hibernate, or if you just want to share a really good joke, you can email Joseph Ottinger at [email protected] , Dave Minter at [email protected], and Jeff Linwood at [email protected] .
Joseph would like to thank various people for encouraging and enabling him to write this book, and so he will: first, his wife and children for allowing him the time and energy to put into the project; various project leads for Hibernate like Steve Ebersole and Chris Cranford; Jonas Konrad, who provided some advice and muchneeded pointers in a few critical spots; friends like Tracy Snell, Andrew Lombardi, Eugene Ciurana, and Justin Lee for serving as sounding boards and source material; his editors, for putting up with his quirks 1 ; bands like Rush, Pink Floyd, and Porcupine Tree for providing the soundtrack to the book 2 ; the letter Z, for having one of the most fun letters’ names to pronounce 3 ; his Gibson guitars, because they care; and lastly, Stan Lee, for giving Joseph a worthwhile impetus to learn to read when he was very young.
Joseph B. Ottinger is a systems architect, consultant, and Knowledge Graph Architect with Cosmify. He’s been messing around with computers since the 1980s, thinking about systems and architecture through war games and music. He’s worked as a developer, architect, consultant, and writer since then, including editing stints at Java Developer Journal and TheServerSide.com.
A musician, programmer, artist, writer, father, and husband, his interests tend to range far and wide, through philosophy, history, science, art, basketball, education, and - apparently - hamsters.
Jeff Linwood has been involved in software programming since he had a 286 in high school. He got caught up with the Internet when he got access to a UNIX shell account, and it has been downhill ever since.
When he’s not playing on the computer, his hobby is running ultramarathons. Jeff is based in Austin, Texas, and helps large companies solve tough problems with content management, search engines, and web application development. Jeff also co-authored Professional Struts Applications (Apress), Building Portals with the Java Portlet API (Apress), and Pro Hibernate 3 (Apress).
Dave Minter has adored computers since he was small enough to play in the boxes they came in. He built his first PC from discarded, faulty, and obsolete components; and he considers that to be the foundation of his career as an integration consultant. Dave is based in London, where he helps large and small companies build systems that “just work.” He wrote Beginning Spring 2: From Novice to Professional (Apress) and co-authored Building Portals with the Java Portlet API (Apress) and Pro Hibernate 3 (Apress).
Massimo Nardone has more than 22 years of experiences in Security, Web/Mobile development, Cloud and IT Architecture. His true IT passions are Security and Android. He has been programming and teaching how to program with Android, Perl, PHP, Java, VB, Python, C/C++ and MySQL for more than 20 years. He holds a Master of Science degree in Computing Science from the University of Salerno, Italy.
He has worked as a Project Manager, Software Engineer, Research Engineer, Chief Security Architect, Information Security Manager, PCI/SCADA Auditor and Senior Lead IT Security/Cloud/SCADA Architect for many years.
His technical skills include: Security, Android, Cloud, Java, MySQL, Drupal, Cobol, Perl, Web and Mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, etc. He currently works as Chief Information Security Office (CISO) for Cargotec Oyj.
He worked as visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki University of Technology (Aalto University). He holds four international patents (PKI, SIP, SAML and Proxy areas).
Massimo has reviewed more than 40 IT books for different publishing company and he is the coauthor of Pro Android Games (Apress, 2015).
1 . Quirks like his endless footnotes; when asked, they allowed Joseph to have one footnote for every three pages, to which the response was, "Thanks! Three footnotes maximum for every page, got it!"
2 . A soundtrack you won’t hear, nor is it likely to be easily detected unless you listen very well. Listen more than that—no, more than that, too. Keep going. Let me know which song you hear, okay?
3 Z is pronounced like “see.” He learned this from his youngest son.