In hibernate we will get data in different ways like:
.get().load()- HQL
- NativeSQL
- etc.
But which one is best in terms of performance?
In hibernate we will get data in different ways like:
.get().load()But which one is best in terms of performance?
Basic rules for fetching data in Hibernate with respect to performance are:
Following these rules requires different approaches in different situations. For example:
Use get() to fetch a single object by its id. Configure fetch types of its relationships (lazy/eager) to achieve optimal balance between rules 1 and 2.
Sometimes you need to obtain a reference to the object, but don't need to fetch it. Use load() in this case to avoid fetching unnecessary data.
Use HQL queries to fetch multiple objects. Tune fetch strategy with join fetch for optimal balance between rules 1 and 2.
Sometimes you need to use different fetch strategies in different scenarios in order to achieve balance between rules 1 and 2, so that static fetch type settings are not enough. In this case use HQL queries tuned for particular scenarios, even when fetching single objects.
Fetch profiles introduced in Hibernate 3.5 may help as well.
Sometimes your DBMS cannot build optimal query plans for queries generated by Hibernate. Use native SQL in this case.