Forewords |
|
xiv | |
Preface |
|
xvii | |
Acknowledgments |
|
xix | |
About this book |
|
xxi | |
About the author |
|
xxiv | |
Authors of Java Persistence with Hibernate, Second Edition |
|
xxv | |
About the cover illustration |
|
xxvi | |
|
Part 1 Getting started with ORM |
|
|
1 | (102) |
|
1 Understanding object/relational persistence |
|
|
3 | (18) |
|
|
4 | (4) |
|
|
4 | (1) |
|
|
5 | (1) |
|
|
6 | (2) |
|
1.2 The paradigm mismatch |
|
|
8 | (9) |
|
The problem of granularity |
|
|
9 | (2) |
|
The problem of inheritance |
|
|
11 | (1) |
|
|
12 | (1) |
|
The problem of associations |
|
|
13 | (2) |
|
The problem of data navigation |
|
|
15 | (2) |
|
1.3 ORM, JPA, Hibernate, and Spring Data |
|
|
17 | (4) |
|
|
21 | (25) |
|
2.1 Introducing Hibernate |
|
|
22 | (1) |
|
2.2 Introducing Spring Data |
|
|
23 | (1) |
|
2.3 "Hello World" with JPA |
|
|
23 | (7) |
|
Configuring a persistence unit |
|
|
25 | (1) |
|
Writing a persistent class |
|
|
26 | (2) |
|
Storing and loading messages |
|
|
28 | (2) |
|
2.4 Native Hibernate configuration |
|
|
30 | (4) |
|
2.5 Switching between JPA and Hibernate |
|
|
34 | (2) |
|
2.6 "Hello World" with Spring Data JPA |
|
|
36 | (4) |
|
2.7 Comparing the approaches of persisting entities |
|
|
40 | (6) |
|
3 Domain models and metadata |
|
|
46 | (57) |
|
3.1 The example CaveatEmptor application |
|
|
47 | (5) |
|
|
47 | (2) |
|
Analyzing the business domain |
|
|
49 | (1) |
|
The CaveatEmptor domain model |
|
|
50 | (2) |
|
3.2 Implementing the domain model |
|
|
52 | (10) |
|
Addressing leakage of concerns |
|
|
52 | (1) |
|
Transparent and automated persistence |
|
|
53 | (1) |
|
Writing persistence-capable classes |
|
|
54 | (3) |
|
Implementing POJO associations |
|
|
57 | (5) |
|
3.3 Domain model metadata |
|
|
62 | (13) |
|
Annotation-based metadata |
|
|
62 | (3) |
|
Applying constraints to Java objects |
|
|
65 | (2) |
|
Externalizing metadata with XML files |
|
|
67 | (2) |
|
Accessing metadata at runtime |
|
|
69 | (5) |
|
Working with Spring Data JPA |
|
|
74 | (1) |
|
4.1 Introducing Spring Data JPA |
|
|
75 | (1) |
|
4.2 Starting a new Spring Data JPA project |
|
|
75 | (3) |
|
4.3 First steps for configuring a Spring Data JPA project |
|
|
78 | (4) |
|
4.4 Defining query methods with Spring Data JPA |
|
|
82 | (5) |
|
4.5 Limiting query results, sorting, and paging |
|
|
87 | (2) |
|
|
89 | (1) |
|
4.7 The @Query annotation |
|
|
90 | (2) |
|
|
92 | (4) |
|
|
96 | (2) |
|
|
98 | (5) |
|
Part 2 Mapping strategies |
|
|
103 | (164) |
|
5 Mapping persistent classes |
|
|
105 | (23) |
|
5.1 Understanding entities and value types |
|
|
106 | (4) |
|
Fine-grained domain models |
|
|
107 | (1) |
|
Defining application concepts |
|
|
107 | (2) |
|
Distinguishing entities and value types |
|
|
109 | (1) |
|
5.2 Mapping entities with identity |
|
|
110 | (11) |
|
Understanding Java identity and equality |
|
|
110 | (1) |
|
A first entity class and mapping |
|
|
111 | (1) |
|
|
112 | (2) |
|
Configuring key generators |
|
|
114 | (4) |
|
Identifier generator strategies |
|
|
118 | (3) |
|
5.3 Entity-mapping options |
|
|
121 | (7) |
|
|
121 | (3) |
|
|
124 | (1) |
|
Making an entity immutable |
|
|
125 | (1) |
|
Mapping an entity to a subselect |
|
|
125 | (3) |
|
|
128 | (42) |
|
6.1 Mapping basic properties |
|
|
129 | (8) |
|
Overriding basic property defaults |
|
|
130 | (1) |
|
Customizing property access |
|
|
131 | (2) |
|
|
133 | (1) |
|
Transforming column values |
|
|
134 | (1) |
|
Generated and default property values |
|
|
135 | (1) |
|
|
136 | (1) |
|
|
136 | (1) |
|
6.2 Mapping embeddable components |
|
|
137 | (10) |
|
|
137 | (1) |
|
Making classes embeddable |
|
|
138 | (3) |
|
Overriding embedded attributes |
|
|
141 | (4) |
|
Mapping nested embedded components |
|
|
145 | (2) |
|
6.3 Mapping Java and SQL types with converters |
|
|
147 | (23) |
|
|
147 | (6) |
|
Creating customJPA converters |
|
|
153 | (9) |
|
Extending Hibernate with User Types |
|
|
162 | (8) |
|
|
170 | (26) |
|
7.1 Table per concrete class with implicit polymorphism |
|
|
171 | (6) |
|
7.2 Table per concrete class with unions |
|
|
177 | (3) |
|
7.3 Table per class hierarchy |
|
|
180 | (3) |
|
7.4 Table per subclass with joins |
|
|
183 | (4) |
|
7.5 Mixing inheritance strategies |
|
|
187 | (2) |
|
7.6 Inheritance of embeddable classes |
|
|
189 | (2) |
|
|
191 | (2) |
|
7.8 Polymorphic associations |
|
|
193 | (3) |
|
Polymorphic many-to-one associations |
|
|
193 | (1) |
|
|
194 | (2) |
|
8 Mapping collections and entity associations |
|
|
196 | (36) |
|
8.1 Sets, bags, lists, and maps of value types |
|
|
197 | (13) |
|
|
197 | (1) |
|
Creating and mapping a collection property |
|
|
198 | (1) |
|
Selecting a collection interface |
|
|
199 | (2) |
|
|
201 | (2) |
|
Mapping an identifier bag |
|
|
203 | (2) |
|
|
205 | (1) |
|
|
206 | (1) |
|
Sorted and ordered collections |
|
|
207 | (3) |
|
8.2 Collections of components |
|
|
210 | (10) |
|
Equality of component instances |
|
|
211 | (1) |
|
|
212 | (3) |
|
|
215 | (1) |
|
|
216 | (1) |
|
|
217 | (2) |
|
Collection in an embeddable component |
|
|
219 | (1) |
|
8.3 Mapping entity associations |
|
|
220 | (12) |
|
The simplest possible association |
|
|
221 | (1) |
|
|
222 | (2) |
|
|
224 | (8) |
|
9 Advanced entity association mappings |
|
|
232 | (35) |
|
9.1 One-to-one associations |
|
|
233 | (11) |
|
|
233 | (5) |
|
The foreign primary key generator |
|
|
238 | (2) |
|
Using a foreign key join column |
|
|
240 | (2) |
|
|
242 | (2) |
|
9.2 One-to-many associations |
|
|
244 | (9) |
|
Considering one-to-many bags |
|
|
244 | (2) |
|
Unidirectional and bidirectional list mappings |
|
|
246 | (2) |
|
Optional one-to-many with a join table |
|
|
248 | (2) |
|
One-to-many association in an embeddable class |
|
|
250 | (3) |
|
9.3 Many-to-many and ternary associations |
|
|
253 | (9) |
|
Unidirectional and bidirectional many-to-many associations |
|
|
253 | (2) |
|
Many-to-many with an intermediate entity |
|
|
255 | (4) |
|
Ternary associations with components |
|
|
259 | (3) |
|
9.4 Entity associations with maps |
|
|
262 | (5) |
|
One-to-many with a property key |
|
|
262 | (1) |
|
Key/value ternary relationship |
|
|
263 | (4) |
|
Part 3 Transactional data processing |
|
|
267 | (120) |
|
|
269 | (25) |
|
10.1 The persistence lifecycle |
|
|
270 | (4) |
|
|
271 | (1) |
|
|
272 | (2) |
|
10.2 The EntityManager interface |
|
|
274 | (11) |
|
The canonical unit of work |
|
|
274 | (2) |
|
|
276 | (1) |
|
Retrieving and modifying persistent data |
|
|
277 | (2) |
|
|
279 | (1) |
|
|
280 | (1) |
|
|
281 | (1) |
|
|
282 | (1) |
|
Caching in the persistence context |
|
|
283 | (1) |
|
Flushing the persistence context |
|
|
284 | (1) |
|
10.3 Working with detached state |
|
|
285 | (9) |
|
The identity of detached instances |
|
|
286 | (2) |
|
Implementing equality methods |
|
|
288 | (3) |
|
Detaching entity instances |
|
|
291 | (1) |
|
|
291 | (3) |
|
11 Transactions and concurrency |
|
|
294 | (37) |
|
11.1 Transaction essentials |
|
|
295 | (1) |
|
|
295 | (1) |
|
Database and system transactions |
|
|
296 | (1) |
|
11.2 Controlling concurrent access |
|
|
296 | (19) |
|
Understanding database-level concurrency |
|
|
297 | (5) |
|
Optimistic concurrency control |
|
|
302 | (8) |
|
Explicit pessimistic locking |
|
|
310 | (3) |
|
|
313 | (2) |
|
11.3 Non-transactional data access |
|
|
315 | (4) |
|
Reading data in auto-commit mode |
|
|
315 | (2) |
|
|
317 | (2) |
|
11.4 Managing transactions with Spring and Spring Data |
|
|
319 | (12) |
|
|
320 | (1) |
|
|
321 | (1) |
|
|
322 | (1) |
|
Programmatic transaction definition |
|
|
322 | (1) |
|
Transactional development with Spring and Spring Data |
|
|
323 | (8) |
|
12 Fetch plans, strategies, and profiles |
|
|
331 | (27) |
|
12.1 Lazy and eager loading |
|
|
332 | (9) |
|
Understanding entity proxies |
|
|
333 | (4) |
|
Lazy persistent collections |
|
|
337 | (2) |
|
Eager loading of associations and collections |
|
|
339 | (2) |
|
12.2 Selecting a fetch strategy |
|
|
341 | (10) |
|
|
341 | (1) |
|
The Cartesian product problem |
|
|
342 | (3) |
|
Prefetching data in batches |
|
|
345 | (2) |
|
Prefetching collections with subsekcts |
|
|
347 | (1) |
|
Eager fetching with multiple SELECTs |
|
|
348 | (1) |
|
|
349 | (2) |
|
12.3 Using fetch profiles |
|
|
351 | (7) |
|
Declaring Hibernate fetch profiles |
|
|
352 | (1) |
|
Working with entity graphs |
|
|
353 | (5) |
|
|
358 | (29) |
|
13.1 Cascading state transitions |
|
|
359 | (7) |
|
Available cascading options |
|
|
360 | (1) |
|
Transitive detachment and merging |
|
|
360 | (3) |
|
|
363 | (2) |
|
|
365 | (1) |
|
13.2 Listening to and intercepting events |
|
|
366 | (8) |
|
JPA event listeners and callbacks |
|
|
366 | (3) |
|
Implementing Hibernate interceptors |
|
|
369 | (4) |
|
|
373 | (1) |
|
13.3 Auditing and versioning with Hibernate Envers |
|
|
374 | (7) |
|
|
375 | (1) |
|
|
376 | (1) |
|
|
377 | (2) |
|
Accessing historical data |
|
|
379 | (2) |
|
13.4 Dynamic data filters |
|
|
381 | (6) |
|
|
381 | (1) |
|
Applying a dynamic filter |
|
|
382 | (1) |
|
Enabling a dynamic filter |
|
|
382 | (2) |
|
Filtering collection access |
|
|
384 | (3) |
|
Part 4 Building Java persistence applications with Spring |
|
|
387 | (76) |
|
14 Integrating JPA and Hibernate with Spring |
|
|
389 | (52) |
|
14.1 Spring Framework and dependency injection |
|
|
390 | (1) |
|
14.2 JPA application using Spring and the DAO pattern |
|
|
391 | (10) |
|
14.3 Generifying aJPA application that uses Spring and DAO |
|
|
401 | (4) |
|
14.4 Hibernate application using Spring and the DAO pattern |
|
|
405 | (4) |
|
14.5 Generifying a Hibernate application that uses Spring and DAO |
|
|
409 | (6) |
|
Working with Spring Data JDBC |
|
|
414 | (1) |
|
15.1 Creating a Spring Data JDBC project |
|
|
415 | (6) |
|
15.2 Working with queries in Spring Data JDBC |
|
|
421 | (9) |
|
Defining query methods with Spring Data JDBC |
|
|
421 | (4) |
|
Limiting query results, sorting, and paging |
|
|
425 | (2) |
|
|
427 | (1) |
|
|
428 | (1) |
|
|
429 | (1) |
|
15.3 Modeling relationships with Spring Data JDBC |
|
|
430 | (11) |
|
Modeling a one-to-one relationship with Spring Data JDBC |
|
|
430 | (2) |
|
Modeling embedded entities with Spring Data JDBC |
|
|
432 | (2) |
|
Modeling a one-to-many relationship with Spring Data JDBC |
|
|
434 | (2) |
|
Modeling a many-to-many relationship with Spring Data JDBC |
|
|
436 | (5) |
|
16 Working with Spring Data REST |
|
|
441 | (22) |
|
16.1 Introducing REST applications |
|
|
442 | (1) |
|
16.2 Creating a Spring Data REST application |
|
|
443 | (6) |
|
16.3 Using ETags for conditional requests |
|
|
449 | (4) |
|
16.4 Limiting access to repositories, methods, and fields |
|
|
453 | (2) |
|
16.5 Working with REST events |
|
|
455 | (4) |
|
Writing an AnnotatedHandler |
|
|
456 | (1) |
|
Writing an ApplicationListener |
|
|
457 | (2) |
|
16.6 Using projections and excerpts |
|
|
459 | (4) |
|
Part 5 Building Java persistence applications with Spring |
|
|
463 | (42) |
|
17 Working urith Spring Data MongoDB |
|
|
465 | (26) |
|
|
465 | (3) |
|
17.2 Introducing Spring Data MongoDB |
|
|
468 | (4) |
|
17.3 Using MongoRepository to access a database |
|
|
472 | (9) |
|
Defining query methods with Spring Data MongoDB |
|
|
472 | (5) |
|
Limiting query results, sorting, and paging |
|
|
477 | (2) |
|
|
479 | (1) |
|
|
480 | (1) |
|
|
481 | (2) |
|
17.5 Referencing other MongoDB documents |
|
|
483 | (2) |
|
17.6 Using MongoTemplate to access a database |
|
|
485 | (6) |
|
Configuring access to the database through MongoTemplate |
|
|
485 | (1) |
|
Executing CRUD operations using MongoTemplate |
|
|
486 | (5) |
|
18 Working with Hibernate OGM |
|
|
491 | (14) |
|
18.1 Introducing Hibernate OGM |
|
|
491 | (1) |
|
18.2 Building a simple MongoDB Hibernate OGM application |
|
|
492 | (10) |
|
Configuring the Hibernate OGM application |
|
|
492 | (2) |
|
|
494 | (2) |
|
Using the application with MongoDB |
|
|
496 | (6) |
|
18.3 Switching to the Neo4j NoSQL database |
|
|
502 | (3) |
|
Part 6 Writing queries and testing Java persistence application |
|
|
505 | (48) |
|
19 Querying JPA with Querydsl |
|
|
507 | (20) |
|
19.1 Introducing Querydsl |
|
|
507 | (1) |
|
19.2 Creating a Querydsl application |
|
|
508 | (6) |
|
Configuring the Querydsl application |
|
|
508 | (3) |
|
|
511 | (1) |
|
Creating the test data to query |
|
|
512 | (2) |
|
19.3 Querying a database with Querydsl |
|
|
514 | (13) |
|
|
516 | (2) |
|
|
518 | (1) |
|
Grouping data and working with aggregates |
|
|
519 | (1) |
|
Working with subqueries and joins |
|
|
520 | (3) |
|
|
523 | (1) |
|
|
524 | (3) |
|
20 Testing Java persistence applications |
|
|
527 | (26) |
|
20.1 Introducing the test pyramid |
|
|
528 | (1) |
|
20.2 Creating the persistence application to test |
|
|
529 | (4) |
|
20.3 Using the Spring TestContext Framework |
|
|
533 | (2) |
|
20.4 The @DirtiesContext annotation |
|
|
535 | (2) |
|
20.5 @Transactional execution |
|
|
537 | (5) |
|
20.6 The @BeforeTransaction and @AfterTransaction annotations |
|
|
542 | (1) |
|
20.7 Working with Spring profiles |
|
|
543 | (4) |
|
20.8 Working with test execution listeners |
|
|
547 | (6) |
Appendix A Maven |
|
553 | (7) |
Appendix B Spring Data JPA keyword usage |
|
560 | (2) |
Appendix C Spring Data JDBC keyword usage |
|
562 | (2) |
Appendix D Spring Data MongoDB keyword usage |
|
564 | (3) |
References |
|
567 | (1) |
Index |
|
568 | |