pwangb wrote:
I have Parent P and Child C. For the following HQL
from C as c where c.parent.uid_pk = ?
Hibernte issues 3 SQL command, it means it hits DB 3 times for this simple query.
Howerver, if I just use jdbc instead of hibernte, I only have 1 db hit.
FALSE.
If one single sql query you will retrieve only the child C where you will have both the parent and the child with Hibernate.
Why so?
Because when building the java instance for the child C, Hibernate needs to set the value of the 'parent' field. This value is an instance of your Parent java class.
If you don't want Hibernate to initialize the Parent when you retrieve a Child, then consider using the 'lazy' keyword on your Parent mapping definition. If the Parent is lazy, HIbernate will instantiate a proxy rather than a concrete class. The actual Parent value will then be loaded from the DB the first time you access it.
On the other hand, both the Child and the Parent could be retrieved in one single query (using outer join) but probably not in your case because you have been using an HQL query to find out the child instance to load.
Have a look at the 3 generated SQL statements and you will understand what is happening. Then you will surely find a way to reduce the amount of DB accesses...
pwangb wrote:
does it mean hibernate has worse performance then pure jdbc connection with more db hits?
Of course not... provided you understand what Hibernate is doing and how to use it.
pwangb wrote:
I am really confused and lost here. Please give me some ideas.
The best ideas you can have is by reading the entire documentation and try by yourself. When you hesitate, try both solutions, turn sql debug on, and see what is happening.