As others have already said, the number of SQL queries should not be the only thing
you should worry about and you should not try to fix something that isn't broken. And always benchmark your application
before and
after your optimization.
The number of queries only becomes an issue, if there are a LOOOOOT of them like it can happen with eager fetching or the n+1 select issue. And even then you have to analyze if the join creates more harm or not.
So let's assume you have had:
The two most common reasons for additional queries are eager fetching and n+1 select issues.
The solution for eager fetching is simple, just switch it to lazy. But if you do that, you have to make sure that you don't create n+1 select issues, if Hibernate needs to initialize these relationships. You can avoid that by defining query specific eager fetching either with fetch joins
or by using
EntityGraphs
You can check out my
free mini-course, if you want to get into more details about finding n+1 select issues with Hibernate and fixing them with entity graphs.