Hibernate in Action
Hibernate in Action
CHRISTIAN BAUER
GAVIN KING
MANNING
Greenwich
(74° w. long.)
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.
209 Bruce Park Avenue Fax: (203) 661-9018
Greenwich, CT 06830 email: manning@manning.com
©2005 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 Manning’s policy to have
the books they publish printed on acid-free paper, and we exert our best efforts to that end.
.
Manning Publications Co. Copyeditor: Tiffany Taylor
209 Bruce Park Avenue Typesetter: Dottie Marsico
Greenwich, CT 06830 Cover designer: Leslie Haimes
ISBN 1932394-15-X
Printed in the United States of America
12345678910–VHG–07 060504
Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
v
contents
foreword xi
preface xiii
acknowledgments xv
about this book xvi
about Hibernate3 and EJB 3 xx
author online xxi
about the title and cover xxii
1
Understanding object/relational persistence 1
1.1 What is persistence? 3
Relational databases 3
■
Understanding SQL 4
■
Using SQL
in Java 5
■
Persistence in object-oriented applications 5
1.2 The paradigm mismatch 7
The problem of granularity 9
■
The problem of subtypes 10
The problem of identity 11
■
Problems relating to associations 13
The problem of object graph navigation 14
■
The cost of the
mismatch 15
1.3 Persistence layers and alternatives 16
Layered architecture 17
■
Hand-coding a persistence layer with
SQL/JDBC 18
■
Using serialization 19
■
Considering EJB
entity beans 20
■
Object-oriented database systems 21
Other options 22
1.4 Object/relational mapping 22
What is ORM? 23
■
Generic ORM problems 25
Why ORM? 26
1.5 Summary 29
Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
vi CONTENTS
2
Introducing and integrating Hibernate 30
2.1 “Hello World” with Hibernate 31
2.2 Understanding the architecture 36
The core interfaces 38
■
Callback interfaces 40
Types 40
■
Extension interfaces 41
2.3 Basic configuration 41
Creating a SessionFactory 42
■
Configuration in
non-managed environments 45
■
Configuration in
managed environments 48
2.4 Advanced configuration settings 51
Using XML-based configuration 51
■
JNDI-bound
SessionFactory 53
■
Logging 54
■
Java Management
Extensions (JMX) 55
2.5 Summary 58
3
Mapping persistent classes 59
3.1 The CaveatEmptor application 60
Analyzing the business domain 61
The CaveatEmptor domain model 61
3.2 Implementing the domain model 64
Addressing leakage of concerns 64
■
Transparent and
automated persistence 65
■
Writing POJOs 67
Implementing POJO associations 69
■
Adding logic to
accessor methods 73
3.3 Defining the mapping metadata 75
Metadata in XML 75
■
Basic property and class
mappings 78
■
Attribute-oriented programming 84
Manipulating metadata at runtime 86
3.4 Understanding object identity 87
Identity versus equality 87
■
Database identity with
Hibernate 88
■
Choosing primary keys 90
3.5 Fine-grained object models 92
Entity and value types 93
■
Using components 93
3.6 Mapping class inheritance 97
Table per concrete class 97
■
Table per class hierarchy 99
Table per subclass 101
■
Choosing a strategy 104
Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>