-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL new objects and fetch outer join
PostPosted: Tue Sep 19, 2006 2:59 am 
Hello! I'm experiencing some problems with HQL query productivity.
Current query looks like the following:


select new ObjectView(a.id, a.name, b, c)
from mypackage.B b
left outer join b.x x
left outer join x.c c
where b.objectId = ?


All domain objects are declared as lazy as well as collections. Currently, there are four queries being executed (log could be :

select from A;
select from B;
select from X
select from C;

I was trying to use fetch join in order to merge everything in one query:


select new ObjectView(a.id, a.name, b, c)
from mypackage.B b
left outer join fetch b.x x
left outer join fetch x.c c
where b.objectId = ?


But update query caused an error "query specified join fetching, but the owner of the fetched association was not present in the select list"
In "Hibernate3 Migration Guide" it mentioned, that furthermore, a query that specifies join fetching, but the owner of the fetched association was not present in the select list throws an exception now: "select b from A join fetch a.bees b" - this query makes no sense, remove the "fetch".

But the only object from the query that not present in select list is X, and fetching is exactly the target I'm trying to achieve.

Are there any solutions?


Top
  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 3:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
select new and join fetch doesn't really make sense.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 8:17 am 
Newbie

Joined: Mon Jun 02, 2008 11:03 am
Posts: 3
Hello,

I am also trying to convert several queries to a single query using select new Object(args).

Previously, I retrieved a list of "Ccr" objects for viewing in a datatable on a JSF page. I then access the properties of these objects in each row of the table using syntax like #{ccr.property}. This works, but ends up requiring about five queries per row of the datatable when I could easily write it as one query for the whole table myself.

Here is the query I have come up with:

Code:
select new domain.removed.mud.helperentity.CcrListItem(ccrAuditFirst, ccrAuditLast, ccr)
from Ccr ccr
left join ccr.ccrAudits ccrAuditFirst
left join ccr.ccrAudits ccrAuditLast
where ccrAuditFirst.timeStamp = (
   select min(ccrAudit_l.timeStamp) from CcrAudit ccrAudit_l
   left join ccrAudit_l.ccr ccr_l
   where ccr_l = ccr
)
and ccrAuditLast.timeStamp = (
   select max(ccrAudit_l.timeStamp) from CcrAudit ccrAudit_l
   left join ccrAudit_l.ccr ccr_l
   where ccr_l = ccr
)


Each Ccr object has a collection called ccrAudits which contains CcrAudit objects. For each Ccr I would like to see the first and last CcrAudit, based on their timestamps. Sometimes the first and last might be the same.

However when this query is run, only the id fields for the three objects in the constructor of the new object are retrieved, and all other information is loaded after that in even more queries than before. I thought it might be that I am aliasing a collection (with one object in it) to a single object in the constructor, but hibernate doesn't seem to mind that and changing the query to a restricted cartesian product doesn't make any difference to the number of queries being run.

I also tried fetch joining as in the manual, but got the following error message:

Code:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list...


Any suggestions would be greatly appreciated,
Dave


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2009 5:54 pm 
Newbie

Joined: Sat Feb 21, 2009 5:28 pm
Posts: 1
Location: Rochester, MN
I am doing some volunteer work for the local school district, and they use Hibernate.

I am also getting the following from a query:
Code:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list


I am trying to run a report on the number of broadcasts the district makes in a month:

Code:
select new PublicBroadcastsPerMonth(month(b.startTime), year(b.startTime), count(b))
from Broadcast as b left join fetch b.broadcastGroup as bg left join fetch bg.channel as c
where c.publicChannel = 1
group by year(b.startTime), month(b.startTime)


The query gives the exception stated above. If I only have Broadcast in the from clause and have no where clause the query works. But we want to limit the query to only broadcasts on public channels which is two tables over.

In the DAO classes, they use get() when retrieving an single entity because they don't want the object to load lazy, and fetch="join" in the XML for the relationships to avoid the n + 1 query problem. Since fetch="join" in the XML is ignored for HQL queries, we use left join fetch in the HQL.

Since the person above mentioned that "select new and join fetch doesn't really make sense", what other options would there be for this scenario where we need the join fetch for the where clause?

Am I missing something obvious?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 7:36 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
The fetch keyword tells hibernate to eager load the association which does not make sense as you are not loading the parent object itself but instantiating a new object from its values.


Top
 Profile  
 
 Post subject: Re: HQL new objects and fetch outer join
PostPosted: Fri Jul 02, 2010 2:22 pm 
Newbie

Joined: Mon May 31, 2010 7:29 am
Posts: 5
It can does not make sense, but im some cases if I donĀ“t use the fetch keyword the object comes lazy im my class constructor. Can it be a mapping problem?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.