Spring Boot Persistence Best Practices Optimize Java Persistence Performance in Spring Boot Applications
Total Page:16
File Type:pdf, Size:1020Kb
Spring Boot Persistence Best Practices Optimize Java Persistence Performance in Spring Boot Applications Anghel Leonard Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in Spring Boot Applications Anghel Leonard Banesti, Romania ISBN-13 (pbk): 978-1-4842-5625-1 ISBN-13 (electronic): 978-1-4842-5626-8 https://doi.org/10.1007/978-1-4842-5626-8 Copyright © 2020 by Anghel Leonard This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please email [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484256251. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper This book is dedicated to my dear wife, Octavia. Table of Contents About the Author ..................................................................................................xxiii About the Technical Reviewer ...............................................................................xxv Introduction .........................................................................................................xxvii Chapter 1: Associations ............................................................................................ 1 Item 1: How to Effectively Shape the @OneToMany Association .................................................. 1 Always Cascade from Parent-Side to Child-Side..................................................................... 2 Don’t Forget to Set mappedBy on the Parent-Side .................................................................. 2 Set orphanRemoval on the Parent-Side .................................................................................. 3 Keep Both Sides of the Association in Sync ............................................................................ 3 Override equals( ) and hashCode( ) ......................................................................................... 4 Use Lazy Fetching on Both Sides of the Association ............................................................... 5 Pay Attention to How toString( ) Is Overridden ........................................................................ 5 Use @JoinColumn to Specify the Join Column Name ............................................................. 5 Author and Book Samples ....................................................................................................... 6 Item 2: Why You Should Avoid the Unidirectional @OneToMany Association ................................ 8 Regular Unidirectional @OneToMany .................................................................................... 10 Using @OrderColumn ............................................................................................................ 15 Using @JoinColumn .............................................................................................................. 19 Item 3: How Efficient Is the Unidirectional @ManyToOne ........................................................... 22 Adding a New Book to a Certain Author ................................................................................ 24 Fetching All Books of an Author ............................................................................................. 26 Paging the Books of an Author .............................................................................................. 27 Fetching All Books of an Author and Adding a New Book ...................................................... 28 Fetching all Books of an Author and Deleting a Book ............................................................ 29 v TABLE OF CONTENTS Item 4: How to Effectively Shape the @ManyToMany Association .............................................. 30 Choose the Owner of the Relationship .................................................................................. 31 Always Use Set not List ......................................................................................................... 31 Keep Both Sides of the Association in Sync .......................................................................... 31 Avoid CascadeType.ALL and CascadeType.REMOVE .............................................................. 31 Setting Up the Join Table ....................................................................................................... 32 Using Lazy Fetching on Both Sides of the Association .......................................................... 32 Override equals( ) and hashCode( ) ....................................................................................... 32 Pay Attention to How toString( ) Is Overridden ...................................................................... 32 Author and Book Samples ..................................................................................................... 32 Item 5: Why Set Is Better than List in @ManyToMany ................................................................. 36 Using List ............................................................................................................................... 36 Using Set ............................................................................................................................... 38 Item 6: Why and When to Avoid Removing Child Entities with CascadeType.Remove and orphanRemoval=true .................................................................................................................. 41 Deleting Authors that Are Already Loaded in the Persistence Context .................................. 45 One Author Has Already Been Loaded in the Persistence Context ........................................ 45 More Authors Have Been Loaded in the Persistence Context ................................................ 46 One Author and His Associated Books Have Been Loaded in the Persistence Context ......... 48 Deleting When the Author and Books that Should Be Deleted Are Not Loaded in the Persistence Context ............................................................................................................... 50 Item 7: How to Fetch Associations via JPA Entity Graphs ........................................................... 52 Defining an Entity Graph via @NamedEntityGraph ................................................................ 53 Ad Hoc Entity Graphs ............................................................................................................. 59 Defining an Entity Graph via EntityManager .......................................................................... 61 Item 8: How to Fetch Associations via Entity Sub- Graphs .......................................................... 62 Using @NamedEntityGraph and @NamedSubgraph ............................................................. 64 Using the Dot Notation (.) in Ad Hoc Entity Graphs ................................................................ 67 Defining an Entity Sub-Graph via EntityManager .................................................................. 70 vi TABLE OF CONTENTS Item 9: How to Handle Entity Graphs and Basic Attributes