Joseph B. Ottinger
Youngsville, NC, USA
Jeff Linwood
Austin, TX, USA
ISBN 978-1-4842-7336-4 e-ISBN 978-1-4842-7337-1
https://doi.org/10.1007/978-1-4842-7337-1
Joseph B. Ottinger, Jeff Linwood and Dave Minter 2022
This work is subject to copyright. All rights are solely and exclusively licensed 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, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
Introduction
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.
Who This Book Is For
This book assumes a good understanding of Java fundamentals and some slight familiarity with database programming using the Java Database Connectivity (JDBC) API. We dont 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 having experience with libraries similar to Hibernate will want to skim the latter half of the book for interesting chapters.
This book uses Java 11, which was the current release when it was drafted, and ignores Javas module system. By print time, Java 17 had become the current long-term supported release, and this book still ignores the module system; while modules are useful (and, arguably, important) their use is tangential at best for the subject matter. For the record, Hibernate works well with Java modules, but this book doesnt cover them at all, because they dont necessarily help one master Hibernate; modules focus is on engineering and creation of better executables, neither subject being a core focus here.
How This Book Is Structured
This book is informally divided into three parts. Chapters addresses the use of Hibernate to talk to nonrelational data stores, providing an easy on ramp to NoSQL.
The following list describes more fully the contents of each chapter:
Chapter 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 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 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 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 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 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 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 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 discusses how Hibernate can be used to make sophisticated queries against the underlying relational database using the built-in Hibernate Query Language (HQL).
Chapter introduces Hibernates filtering capabilities, allowing easy and convenient selection of subsets of data.
Chapter demonstrates some core concepts in integrating Hibernate into a full-stack architecture, leveraging the Undertow servlet engine as a container.