Reputation: 6787
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.
Upvotes: 1
Views: 546
Reputation: 33785
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.
Upvotes: 1