0

In hibernate we will get data in different ways like:

  1. .get()
  2. .load()
  3. HQL
  4. NativeSQL
  5. etc.

But which one is best in terms of performance?

1
  • 1
    Welcome to Stack Overflow. I'm afraid we will not be able to help you since your question lacks detail. Please read stackoverflow.com/questions/how-to-ask Commented Feb 3, 2012 at 11:17

1 Answer 1

1

Basic rules for fetching data in Hibernate with respect to performance are:

  1. Don't fetch more data than you need
  2. Fetch data that you need with minimal number of queries
  3. Make sure that DBMS generates optimal query plans for your queries

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.