Hibernate in Action
Hibernate in Action
contents foreword xi preface xiii about this book about Hibernate3 and EJB 3 xx author online xxi about the title and cover xxii 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 prou ntity relating to associ 13 The problem of object graph navigation 14.The cost of the mismatch 15 1.3 Persistence layers and alternatives 16 Lavered architecture 17.Hand-coding sistence layer with 18 sing se a 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
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 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·NDI-bound SessionFactory 53.Logging 54.Java Management Extensions (JMX)55 2.5 Summary 58 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 ersistence 65.Writing POJOs 67 Implementing POIO associations 69Adding logic to accessor methods 73 3.3 Defining the mapping metadata 75 Metadata in XML 75.Basic property and class rogramming 84 3.4 Understanding object identity 87 Hibernate88·Choosing prima町ykgs90 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 97Table per class hierarchy 99 Table per subdlass 101.Choosing a strategy 10g
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>
CONTENTS 3.7 Introducing associations 105 Managed associations?106.Multiplicity 106 The simplest possible association 107.Making the association 111 3.8 Summary 112 Working with persistent objects 114 4.1 The persistence lifecycle 115 Transient objects 116.Persistent objects 117.Detached 119.Outside the 122 4.2 The persistence manager 126 Making an object persistent 126.Updating the persistent state of a detached instance 127.Retrieving a persistent object 129 Updating a persistent object 129.Making a persistent object transien 130 4.3 Using transitive persistence in Hibernate 131 Persistence by reachability 131.Cascading persistence with Hibernate 133.Managing auction categories 134 Distinguishing between transient and detached instances 138 4.4 Retrieving objects 139 Retrieving objects by identifier 140.Introducing HQL 141 QueyL riteria by example 143.Fetching strategies 143.Selecting a fetching strategy in mappings 146 Tuning object retrieval 151 4.5 Summary 152 Transactions,concurrency,and caching 154 5.1 Transactions,concurrency.and caching 154 5.2 Understanding database transactions 156 JDBC and TA transactions 157.The Hibernate Transaction API 158.Flushing the Session 160.Understanding isolation levels 161.Choosing an isolation level 163.Setting an isolation level 165.Using pessimistic locking 165 5.3 Working with application transactions 168 Using managed versioning 169Granularity of a Session 172.Other ways to implement optimistic locking 174
CONTENTS vii 3.7 Introducing associations 105 Managed associations? 106 ■ Multiplicity 106 The simplest possible association 107 ■ Making the association bidirectional 108 ■ A parent/child relationship 111 3.8 Summary 112 4 Working with persistent objects 114 4.1 The persistence lifecycle 115 Transient objects 116 ■ Persistent objects 117 ■ Detached objects 118 ■ The scope of object identity 119 ■ Outside the identity scope 121 ■ Implementing equals() and hashCode() 122 4.2 The persistence manager 126 Making an object persistent 126 ■ Updating the persistent state of a detached instance 127 ■ Retrieving a persistent object 129 Updating a persistent object 129 ■ Making a persistent object transient 129 ■ Making a detached object transient 130 4.3 Using transitive persistence in Hibernate 131 Persistence by reachability 131 ■ Cascading persistence with Hibernate 133 ■ Managing auction categories 134 Distinguishing between transient and detached instances 138 4.4 Retrieving objects 139 Retrieving objects by identifier 140 ■ Introducing HQL 141 Query by criteria 142 ■ Query by example 143 ■ Fetching strategies 143 ■ Selecting a fetching strategy in mappings 146 Tuning object retrieval 151 4.5 Summary 152 5 Transactions, concurrency, and caching 154 5.1 Transactions, concurrency, and caching 154 5.2 Understanding database transactions 156 JDBC and JTA transactions 157 ■ The Hibernate Transaction API 158 ■ Flushing the Session 160 ■ Understanding isolation levels 161 ■ Choosing an isolation level 163 ■ Setting an isolation level 165 ■ Using pessimistic locking 165 5.3 Working with application transactions 168 Using managed versioning 169 ■ Granularity of a Session 172 ■ Other ways to implement optimistic locking 174 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>
CONTENTS 5.4 Caching theory and practice 175 strategies and scopes 176.The Hibernale cach 179Caching in practice 185 5.5 Summary 194 Advanced mapping concepts 195 6.1 Understanding the Hibernate type system 196 Buil-in mapping types 198 Using mapping types 200 6.2 Mapping collections of value types 211 Sets,bags,lists,and maps 211 6.3 Mapping entity associations 220 Oneto-one associations 220.Many-to-many associations 225 6.4 Mapping polymorphic associations 234 Polymorphic many-to-one associations 234Polymorphi collections 236.Polymorphic associations and table-per- concrete-class 237 6.5 Summary 239 Retrieving objects efficiently 241 7.1 Executing queries 243 The query interfaces 243.Binding parameters 245 Using named queries 249 7.2 Basic queries for objects 250 The simplest quer町y250·Using aliases251·Polymorphic queries 251.Restriction 252.Comparison operators 253 String matching 255.Logical operators 256.Ordering query results 257 7.3 Joining associations 258 Hibernate join options 259.Fetching associations 260 Using aliases with joins 262.Using implicit joins 265 Theta-style joins 267.Comparing identifiers 268 7.4 Writing report queries 269 Projection 270.Using aggregation 272.Grouping 273 Restricting groups with having 274.Improving performance with report queries 275
viii CONTENTS 5.4 Caching theory and practice 175 Caching strategies and scopes 176 ■ The Hibernate cache architecture 179 ■ Caching in practice 185 5.5 Summary 194 6 Advanced mapping concepts 195 6.1 Understanding the Hibernate type system 196 Built-in mapping types 198 ■ Using mapping types 200 6.2 Mapping collections of value types 211 Sets, bags, lists, and maps 211 6.3 Mapping entity associations 220 One-to-one associations 220 ■ Many-to-many associations 225 6.4 Mapping polymorphic associations 234 Polymorphic many-to-one associations 234 ■ Polymorphic collections 236 ■ Polymorphic associations and table-perconcrete-class 237 6.5 Summary 239 7 Retrieving objects efficiently 241 7.1 Executing queries 243 The query interfaces 243 ■ Binding parameters 245 Using named queries 249 7.2 Basic queries for objects 250 The simplest query 250 ■ Using aliases 251 ■ Polymorphic queries 251 ■ Restriction 252 ■ Comparison operators 253 String matching 255 ■ Logical operators 256 ■ Ordering query results 257 7.3 Joining associations 258 Hibernate join options 259 ■ Fetching associations 260 Using aliases with joins 262 ■ Using implicit joins 265 Theta-style joins 267 ■ Comparing identifiers 268 7.4 Writing report queries 269 Projection 270 ■ Using aggregation 272 ■ Grouping 273 Restricting groups with having 274 ■ Improving performance with report queries 275 Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>