Java – why don’t I use @ batchsize in every lazy loaded relationship?
Hibernate's @ batchsize annotation allows batch fetching of deferred loaded entities For example If I had something like this:
public class Product { @OneToMany(fetchType=LAZY) @BatchSize(size=10) private ProductCategory category; }
Now, if I get a product category, hibernate will get up to ten product categories. These products have not initialized their category fields in the current session This can save a lot of SQL calls to the database So far, it's very good Now I don't know why I won't use @ batchsize annotation in every lazy relationship? After all, why call the database? Obviously, there must be a reason, otherwise hibernate guys may take it as the default, but I can't see it at present
Solution
I won't answer your question directly, but I will answer a more general question, which may be "I found something faster for me. Why not apply it everywhere?
The short answer is: you shouldn't do first mover optimization
Hibernate is a great ORM that allows various optimizations You should measure all the processes that cause the problem (classic n + 1, even fast, any slow process, etc.) and optimize them to solve the problem
You may want to get better performance by loading some properties because you always use them, so you may need a batchsize of 100 because you know that this is the number of relationships about the property
Ultimately, you should not care about optimization unless you need to care about it When you complete the measurement and find problems, you need to care