| Table of Contents | 5 |
|---|
| About the Author | 23 |
|---|
| About the Technical Reviewer | 24 |
|---|
| Introduction | 25 |
|---|
| Chapter 1: Associations | 29 |
|---|
| Item 1: How to Effectively Shape the @OneToMany Association | 29 |
| Always Cascade from Parent-Side to Child-Side | 30 |
| Don’t Forget to Set mappedBy on the Parent-Side | 30 |
| Set orphanRemoval on the Parent-Side | 31 |
| Keep Both Sides of the Association in Sync | 31 |
| Override equals() and hashCode() | 32 |
| Use Lazy Fetching on Both Sides of the Association | 33 |
| Pay Attention to How toString() Is Overridden | 33 |
| Use @JoinColumn to Specify the Join Column Name | 33 |
| Author and Book Samples | 34 |
| Item 2: Why You Should Avoid the Unidirectional @OneToMany Association | 36 |
| Regular Unidirectional @OneToMany | 38 |
| Persisting an Author and Their Books | 38 |
| Persisting a New Book of an Existing Author | 40 |
| Deleting the Last book | 41 |
| Deleting the First Book | 42 |
| Using @OrderColumn | 43 |
| Persist the Author and Books | 44 |
| Persist a New Book of an Existing Author | 45 |
| Delete the Last Book | 45 |
| Delete the First Book | 46 |
| Using @JoinColumn | 47 |
| Persist the Author and Books | 47 |
| Persist a New Book of an Existing Author | 48 |
| Delete the Last Book | 49 |
| Delete the First Book | 49 |
| Item 3: How Efficient Is the Unidirectional @ManyToOne | 50 |
| Adding a New Book to a Certain Author | 52 |
| Fetching All Books of an Author | 54 |
| Paging the Books of an Author | 55 |
| Fetching All Books of an Author and Adding a New Book | 56 |
| Fetching all Books of an Author and Deleting a Book | 57 |
| Item 4: How to Effectively Shape the @ManyToMany Association | 58 |
| Choose the Owner of the Relationship | 59 |
| Always Use Set not List | 59 |
| Keep Both Sides of the Association in Sync | 59 |
| Avoid CascadeType.ALL and CascadeType.REMOVE | 59 |
| Setting Up the Join Table | 60 |
| Using Lazy Fetching on Both Sides of the Association | 60 |
| Override equals() and hashCode() | 60 |
| Pay Attention to How toString() Is Overridden | 60 |
| Author and Book Samples | 60 |
| Item 5: Why Set Is Better than List in @ManyToMany | 64 |
| Using List | 64 |
| Using Set | 66 |
| Preserving the Order of the Result Set | 67 |
| Using @OrderBy | 68 |
| Item 6: Why and When to Avoid Removing Child Entities with CascadeType.Remove and orphanRemoval=true | 69 |
| Deleting Authors that Are Already Loaded in the Persistence Context | 73 |
| One Author Has Already Been Loaded in the Persistence Context | 73 |
| More Authors Have Been Loaded in the Persistence Context | 74 |
| One Author and His Associated Books Have Been Loaded in the Persistence Context | 76 |
| Deleting When the Author and Books that Should Be Deleted Are Not Loaded in the Persistence Context | 78 |
| Item 7: How to Fetch Associations via JPA Entity Graphs | 80 |
| Defining an Entity Graph via @NamedEntityGraph | 81 |
| Overriding a Query Method | 82 |
| Using the Query Builder Mechanism | 83 |
| Using Specification | 84 |
| Using @Query and JPQL | 85 |
| Ad Hoc Entity Graphs | 87 |
| Defining an Entity Graph via EntityManager | 89 |
| Item 8: How to Fetch Associations via Entity Sub-Graphs | 90 |
| Using @NamedEntityGraph and @NamedSubgraph | 92 |
| Using the Dot Notation (.) in Ad Hoc Entity Graphs | 95 |
| Defining an Entity Sub-Graph via EntityManager | 98 |
| Item 9: How to Handle Entity Graphs and Basic Attributes | 98 |
| Item 10: How to Filter Associations via a Hibernate-Specific @Where Annotation | 103 |
| Item 11: How to Optimize Unidirectional/Bidirectional @OneToOne via @MapsId | 107 |
| Regular Unidirectional @OneToOne | 107 |
| Regular Bidirectional @OneToOne | 110 |
| @MapsId to the Rescue of @OneToOne | 112 |
| Item 12: How to Validate that Only One Association Is Non-Null | 115 |
| Testing Time | 117 |
<