These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: How to optimize HQL queries
PostPosted: Thu Sep 01, 2005 2:08 pm 
Newbie

Joined: Thu Sep 01, 2005 12:39 pm
Posts: 16
Location: porto alegre, brazil
hi there!
I've been searching for ways to optmize my HQL queries. I was able to reduce the number of selects with the "left join fetch", but not like in the examples I found. They [the examples] shows the query in the form "select <columns> from <table> where <condition> left join fetch <relation>", but always I did like this I got the syntax error , now my queries are like "from <table> left join fetch <relation> where condition>" - I wander if the two forms are equivalent.

tutorial site: http://www.javalobby.org/articles/hiber ... e=archives

Now I want to reduce the number of columns retrivied with the "select new" type of query. But I've some doubts I haven't been able to elucidate.

1) The queries are based on DAO's classes - what I would like to do is instantiate the constructors on the concrete classes - the POJO's - of course, remebering of the empty constructor Hibernate needs to creat the objects, - there is a way to do this or I need to create new (simple - just the fields that representing the columns I need) POJO's and new DAO's classes? or just the creation of a POJO is enough?

2)How a reference the class in the constructor on the HQL string query? I use the getClassName followed by an alias to reference the class in my string queries - so it would by like "select <alias>(<alias>.field1,...,<alias>.fieldn) from getClassName() <alias>"?

3) Can I create as many constructor as I need in the same class to use in the HQL query? Or do I really need to create a class for each constructor used in the query? This sucks, besides, i will loose on efficiency - There are any paper comparing the efficiency of the various form of querying?
Thanks for all.

_________________
Bsc. Tiago Martins
software engineer

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: How to optimize HQL queries
PostPosted: Thu Sep 01, 2005 2:28 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
You can specify as many constructors as you wish. Hibernate uses the default constructor for basic use, and whatever constructor fits the query for the others.

Code:
select new MyObject(a.valueA1, a.valueA2, b.valueB) from ObjectA a, ObjectB b where a.someId = b.someId


I think there is an issue preventing the use of Collections in the constructor. So you couldn't do something like

Code:
select new MyObject(a.valueA1, a.kittens) from ObjectA a


In this case, I believe you would have to do
Code:
select new MyObject(a) from ObjectA a....


But I haven't tried this in a few months so it might have been added. Or I could just be remembering it wrong.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 2:12 pm 
Newbie

Joined: Thu Sep 01, 2005 12:39 pm
Posts: 16
Location: porto alegre, brazil
ok - but i have to mapping the MyObject class?

_________________
Bsc. Tiago Martins
software engineer

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 3:36 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Nope, the MyObject class doesn't have to be mapped, you just need the required constructors.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 9:58 am 
Newbie

Joined: Thu Sep 01, 2005 12:39 pm
Posts: 16
Location: porto alegre, brazil
sorry, I forgot to say, I've been using the spring framework with hibernate - thats why i think the MyObject class need to be maped, cos' I've tried it before, and I get "no class found" exception triggered by spring.

_________________
Bsc. Tiago Martins
software engineer

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 10:03 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
That sounds like a classpath issue rather than a mapping issue to me, but then again I've never used Spring.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 12:55 pm 
Newbie

Joined: Thu Sep 01, 2005 12:39 pm
Posts: 16
Location: porto alegre, brazil
it was a path mistake, I forgot to inform the path of the package of MyObject class. This solution is very straightforward for me, that have to optmize the whole HQL queries, since the system is ready, I don't need to do big changes in its code.

_________________
Bsc. Tiago Martins
software engineer

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 1:51 pm 
Newbie

Joined: Thu Sep 01, 2005 12:39 pm
Posts: 16
Location: porto alegre, brazil
I was wandering - Isn't enough setting hibernate.use_outer_join to true the same as using left join fetch?Cos "left join fetch" is as good as "select new" in some circustances.

_________________
Bsc. Tiago Martins
software engineer

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 8:28 pm 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
tferraz wrote:
it was a path mistake, I forgot to inform the path of the package of MyObject class. This solution is very straightforward for me, that have to optmize the whole HQL queries, since the system is ready, I don't need to do big changes in its code.


Hello!

Could you tell me how you solved your problem?
Indeed, I'm working under Eclipse and I created my object in a new package but in the source folder src.

I have the following exception net.sf.hibernate.QueryException: class not found and I don't know what to do...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 9:40 pm 
Newbie

Joined: Thu Sep 01, 2005 12:39 pm
Posts: 16
Location: porto alegre, brazil
hi there!!!
It was needed to write the whole path ( package way) that the class is into.
( yes, I finished a sentence with a preposition :P )
something like:

select new br.srv.dao.concreteclass.MyObject(....) from ....

_________________
Bsc. Tiago Martins
software engineer

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 10, 2005 10:52 am 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
Hello!

Ok I understood now!

Thankx a lot, it works!

If you are Portuguese (because of your name) , I would say "Obrigado!", otherwise "Thank You!" :D


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 7:51 am 
Newbie

Joined: Thu Sep 01, 2005 12:39 pm
Posts: 16
Location: porto alegre, brazil
de nada :)

_________________
Bsc. Tiago Martins
software engineer

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.