Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HQL Problem

 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When my method retrieveOfferList() which does some query through criteria
and return result via criteria.list().

But there are 34 HQL statement generated by Hibernate instead of just 1 HQL statement when calling retrieveOfferList () .

I guess the reason is as below listed:

When calling retrieveOfferList(), Hibernate will firstly build one HQL statatment to get all related Offers, in my test case is 33 Offer objects.

But each Offer object has a attribute which is TermsandConditions object stored in TermsandConditions Table , the relation between Offer Table and TermsandContions Table is one-to-one mapping.

So for initializing 33 Offer objects, Hibernate will create other 33 HQL statments to get associated TermsAndConditions Object

Those 33 additionally generated HQLs will be speed killer?
If so. How to prevent it? Through defining lazy load=true?

How to prevent those 33 HQLs from being generated? Actually I dont need
to fill Offer object's field which is termsandconditions object.

Thanks in advance
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaRanch forum is for questions related to our site. Since this is about Hibernate, I am moving it to the ORM forum.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is based on your fetching strategy. In your Criteria object you can set it to do eager fetching with FetchMode.EAGER, and it will do it all in one query.

Mark
 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i didnot change my fetchmode. Will it effect the speed ?

Thanks and Rgds
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the question is do you think that running 33+ queries to the database versus 1 query will affect performance?

You can also set your relationship in the mapping file to lazy = "false.

Mark
 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote test case

Person and Employee (one to one mapping), in my mapping file
insert 10 records in Person Table 10 records in Employee Table

I test with below 2 methods. one is with lazyload, one is with eagerfetch
result is
For lazyload. spend 297 ms
For eagerload spend 250 ms.



public static void searchWithLazyLoad(){

Transaction tx = SessionManager.currentSession().beginTransaction();
long lazystart=System.currentTimeMillis();
Criteria crit=SessionManager.currentSession().createCriteria(Person.class);
System.out.println("Person number is " +crit.list());
long lazyend=System.currentTimeMillis();
tx.commit();
SessionManager.closeSession();
System.out.println("lazy fetch time is : " + (lazyend-lazystart) );
}

public static void searchWithEagerFetch(){
Transaction tx = SessionManager.currentSession().beginTransaction();
long eagerstart=System.currentTimeMillis();
Criteria crit=SessionManager.currentSession().createCriteria(Person.class).setFetchMode("employee",FetchMode.JOIN);
System.out.println("Person number is " +crit.list());
long eagerend=System.currentTimeMillis();
tx.commit();
SessionManager.closeSession();
System.out.println("eager fetch time is : " + (eagerend-eagerstart));
}
 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I test it further

set lazy=false, will not generate additonal HQL ,but doesnot improve speed.
set fetch="join" will improve speed and disable additonal HQL generation.
cos it will fetch related object via join instead of select statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic