Java Persistence with Hibernate, Second Edition
Christian Bauer, Gavin King, and Gary Gregory
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email:
orders@manning.com2016 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co.20 Baldwin RoadPO Box 761Shelter Island, NY 11964 | Development editor: Christina TaylorTechnical development editor: Palak MathurCopyeditor: Tiffany TaylorProofreaders: Katie Tennant, Barbara MireckiTechnical proofreader: Christian AlfanoTypesetter: Dottie MarsicoCover designer: Marija Tudor |
ISBN 9781617290459
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 EBM 20 19 18 17 16 15
Dedication
To Alexander, for teaching me how to teach him
GG
Brief Table of Contents
Table of Contents
Praise for the First Edition
The definitive guide to Hibernate and to object/relational mapping in enterprise computing.
From the Foreword by Linda DeMichiel
Sun Microsystems
This book is the ultimate solution. If you are going to use Hibernate in your application, you have no other choice; go rush to the store and get this book.
Meera Subbarao
JavaLobby
Java Persistence with Hibernate is the most comprehensive, authoritative, and definitive guide, tutorial, and reference to OR-mapped Java persistence.
Will Wagers
C#online.net
A definitive source on Hibernate. Great for any developer.
Patrick Peak, CTO
BrowserMedia, Author of Hibernate Quickly
I wholeheartedly recommend this book!
Stuart Caborn
ThoughtWorks
Great topic, great contentand there is a great need for this book!
Ryan Daigle, RTP Region
ALTERthought
This is the most complete book on Hibernate on the market. It covers everything, and I mean everything. From mapping to annotations to whatever ... its in here.
Liz Hills
Amazon reviewer
Foreword to the First Edition
Relational databases are indisputably at the core of the modern enterprise. While modern programming languages, including Java, 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 modelover earlier navigational models as well as over later OODB modelsis 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 challengeto provide a bridge between relational data and Java objectsthat 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. Hibernates developers designed it with modularity, pluggability, extensibility, and user customization in mind. As a result, in the few years since its initial release, Hibernate has rapidly become one of the leading ORM technologies for enterprise developersand 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.
L INDA D EMICHIEL
L EAD A RCHITECT , E NTERPRISE J AVABEANS
S UN M ICROSYSTEMS
N OVEMBER 2012
Preface
This is our third book about Hibernate, an open source project that is almost 15 years old. In a recent poll, Hibernate was among the top five tools used by many Java developers every day. This shows that SQL databases are still the preferred technology for reliable data storage and management, especially in the Java enterprise software development space. Its also a testament to the quality of specifications and tools available, which today make it easy to start a project and to estimate and reduce risk when building large, complex applications.
The fifth major Hibernate release is now available, as well as the second major version of the Java Persistence API specification (JPA) implemented by Hibernate. The core of Hibernate, or what is now called object/relational mapping (ORM), has been mature for a long time, and many small improvements have been made over the years. Other related projects such as Hibernate Search, Hibernate Bean Validation, and more recently Hibernate object/grid mapping (OGM) are delivering new and innovative solutions that make Hibernate a complete tool kit for a diverse range of data-management tasks.
When we wrote the previous edition of this book, Hibernate was undergoing some significant changes: grown organically and driven by an open source community and the daily requirements of Java developers, Hibernate had to become more formal and implement the first version of the JPA specification. The last edition was therefore a large book, because many examples had to be shown in the old form and the new, standardized form.