Coffee with Thorben 2020-02-26 – Utility classes that make your life easier
Take your skills to the next level!
The Persistence Hub is the place to be for every Java developer. It gives you access to all my premium video courses, monthly Java Persistence News, monthly coding problems, and regular expert sessions.
Utility classes that make your life easier
Implementing a persistence layer with Hibernate can require a lot of String references:
- You’re referencing entities and their attributes by their name.
- Query hints have very long names that you need to provide as a String.
- When registering an entity graph, you need to provide a String that defines if it will be used as a fetch or load graph.
To make working with these Strings a little easier, JPA and Hibernate provide utility classes and code generators. In this stream, we will take a closer look at the most commonly options.
Live Online Workshops
If you enjoyed the live stream, you will love my live online workshops. Find out more at thorben-janssen.com/workshops.
Question of the Week
This week, I answered a question on StackOverflow about how Hibernate handles superclasses if you annotate them with @MappedSuperclass. This mapping is pretty simple but you should be aware of its limitations:
Getting An Instance Of A Mapped Superclass With Hibernate EntityManager.
This week’s tutorials
Blog
The 2 most common mistakes when optimizing your persistence layer’s performance are applying general best practices and following the advice of static code analyzers blindly. And that’s not because all these recommendations are entirely wrong.
It’s because none of them are tailored to your application and the data it manages. A good part of these general recommendations isn’t relevant to your application. Most application-specific issues don’t show up in these lists. So, you’re spending your time fixing problems that don’t provide any value to your users instead of working on the critical ones.
There are better ways to improve the performance of your application. In this week’s post, I talk about the right performance tuning mindset, the information you need, and the actions you can take: Hibernate Performance Tuning Done Right.
YouTube
One of the most important topics when talking about performance optimizations for Spring Data JPA is handling associations. As a rule of thumb, you should always use lazy fetching for all associations and combine it with query-specific fetching.
In this week’s video, I show you how to do that using Spring Data JPA. As so often, it makes things a little easier than plain JPA: How to Initialize Entity Associations with Spring Data JPA