1

I want to understand how hibernate execute hql query internally or in other models how hql query engine works. Please suggest some good links for same?

One of reason for reading is following problem.

    Class Branch
    {

          //lazy loaded
          @joincolumn(name="company_id")
          Company company;
    } 

Since company is heavy object so it is lazy loaded.

now i have hql query

       "from Branch as branch where branch.Company.id=:companyId"

my concern is that if for firing above query, hql engine has to retrieve company object then its a performance hit and i would prefer to add one more property in Branch class i.e. companyId. So in this case hql query would be

    "from Branch as branch where branch.companyId=:companyId"

If hql engine first generate sql from hql followed by firing of sql query itself, then there should be no performance issue.

Please let me know if problem is not understandable.

Maddy.Shik
  • 6,241
  • 15
  • 62
  • 98
  • 1
    I don't think hibernate will fetch the company object even though you use it in the query itself. You can verify that by taking a look at the generated SQL – André Pena Dec 26 '10 at 23:30
  • @Ciwee: Thanks, where to see generated sql? – Maddy.Shik Dec 26 '10 at 23:35
  • It's the show_sql configuration: http://www.google.com.br/#hl=pt-BR&biw=1680&bih=949&&sa=X&ei=jdMXTae8HYP98Aaqz7GkDg&ved=0CBYQBSgA&q=hibernate+show_sql&spell=1&fp=dd6b8383160ea132 – André Pena Dec 26 '10 at 23:46

1 Answers1

1

My concern is that if for firing above query, hql engine has to retrieve company object then its a performance hit

Since you want just to use the Company identifier (in plain SQL, a single foreign key), Hibernate engine is smart enough to use Company foreign key without loading the Company entity

But...

It just work when using property access strategy. On the other hand, you have to set up your entity by putting each JPA related annotation right above the getter method (instead of the field - as shown by your example). It occurs because Hibernate, behind the scenes, makes use of proxies by subclassing your entities. If you use, for instance, a field named id encapsulated by a property called userName (yes, it is possible) such as

 @Id
 private String id;

 public String getUserName() { return this.id; }
 public void setUserName(String userName) { this.id = userName; }

Because the logic used by Hibernate to manage your entities is placed inside its subclass properties, How can Hibernate suppose that userName property encapsulates a field named id ???

So if you want to avoid loading Company, use property access strategy instead.

Arthur Ronald
  • 31,649
  • 18
  • 106
  • 135
  • thank you so much for answer. Actually i am using property access strategy only, above example was only for understanding. but still i am not able to understand reason for same. Can u please send me some link to understand how Hibernate works behind the scenes from basics. – Maddy.Shik Dec 27 '10 at 16:48
  • @Maddy.Shik This answer can help you: http://stackoverflow.com/questions/1607532/1608621#1608621 Other resource is Java Persistence with Hibernate book – Arthur Ronald Dec 27 '10 at 18:04