In a nutshell, this book is a collection of best practices for Java persistence performance in Spring Boot applications. These practices have been exposed via 120+ items, and they can be grouped into three categories:
First, we discuss the best practices for defining entities, mapping relationships, writing queries, fetching data, choosing identifiers generators, and so on. Mainly, we cover the areas where Spring Boot cannot help you with built-in artifacts and where you can avoid important performance penalties that are hard to fix and may require significant changes in your domain model.
Second, we address best practices for using Spring Boot support (more precisely, Spring Data). As you will see, relying on built-in support as a silver bullet can come with performance penalties. For example, using the Open Session in View, offset pagination, post-commits hooks, or misunderstanding @Transactional are just a few of the covered topics. Im pretty sure that you are ready and impatient to jump into this category of items.
Third, we deep dive into several Hibernate goodies that can sustain the performance of your applications. By default, Spring Data relies on Hibernate as its persistence provider, therefore you can exploit Hibernate via Spring Data, and you can exploit underlying Hibernate goodies as well. Goodies such as populating a child-side parent association via a Hibernate proxy, using Dirty Tracking, delaying connection acquisition, lazy loading attributes, and using natural keys are just a few of the covered items.
The prerequisites of this book are pretty intuitive. Youll need an IDE (e.g., NetBeans, Eclipse, IntelliJ IDEA, Visual Studio, etc.), MySQL, and PostgreSQL. Optionally, you may install or use other database vendors as well (e.g., Oracle, SQL Server, etc.).
As you will see, I prefer to add @Repository annotation at repository interface level. Is well-known that @Repository is useful for translating the unchecked SQL specific exception to Spring exceptions. This way, we have to handle only DataAccessException (and its subclasses).
Nevertheless, this is a valid statement in general when we use Spring, but Spring Data repositories are already backed by a Spring proxy. In other words, using @Repository doesnt make any difference. I prefer to use it for avoiding any confusions and simpy highlight the repository interfaces, but if you consider this too verbose or just noise then feel free to remove it.
In an overwhelming percentage the examples used in this book uses Hibernate JPA. In other words, we boostrap Hibernate as the JPA provider which is the most common use case in Spring Boot applications that uses Spring Data JPA. If your Sping Boot (Spring) application boostraps Hibernate nativelly (e.g., via SessionFactoryBuilder , BootstrapServiceRegistryBuilder , SessionRegistry , Configuration , HibernateTransactionManager , etc.) then, depending on the case/scenario, you may notice different behaviors.
In this book, when you encounter Hibernate-specific or Hibernate ORM then I refer to something that doesnt exist in JPA (exist only in Hibernate) and it might not be that obvious in the corresponding context.
For brevitys sake and in order to avoid cluttering the climax of topics you will see several shortcomings in code that should be avoided in production as follows:
hard-coded identifiers (primary keys) or other data that is a good candidate for being arguments of metods
usage of orElseThrow() for unwrapping Optional objects (I prefer this approach because it quicky signals if is something wrong with finding/loading the requested data)
maybe something else that I forgot to mention here
Main performance penalties
Use eager fetching
Items: 1-5, 7, 8, 9, 23, 24
Dont prevent/fix N+1 issues
Items: 6-9, 23, 24, 39-41, 43, 56, 66, 67, 81, 108
Fetch more data than needed
Items: 10, 12, 21, 23-38, 42, 43, 45, 56, 57, 97, 98, 105, 128
Update/deletes huge lists of elements one-by-one
Items: 6, 51-53, 59, 109, 126, 129
Use entities for read-only operations
Items: 16, 22, 25-38, 42, 43, 56, 57, 95, 96
Implement low-performing batching
Implement low-performing associations
Items: 1-5, 11, 12, 14, 75, 76, 80
Use low-performing identifiers
Use low-performing pagination