-->
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.  [ 4 posts ] 
Author Message
 Post subject: Why is this an illegal query?
PostPosted: Wed Jan 31, 2007 4:02 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I'm getting the following exception:


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


[FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=net.iss.sp.entity.Asset.networkInterfaces,tableName=NetworkInterface,tableAlias=networkint1_,origin=Asset asset0_,colums={asset0_.AssetID ,className=net.iss.sp.entity.NetworkInterface}}]


[select count( asset ) from net.iss.sp.entity.Asset asset left join fetch asset.networkInterfaces left join fetch asset.owner left join fetch asset.criticality ];


nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list

[FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=net.iss.sp.entity.Asset.networkInterfaces,tableName=NetworkInterface,tableAlias=networkint1_,origin=Asset asset0_,colums={asset0_.AssetID ,className=net.iss.sp.entity.NetworkInterface}}]


[select count( asset ) from net.iss.sp.entity.Asset asset left join fetch asset.networkInterfaces left join fetch asset.owner left join fetch asset.criticality ]


I've read the hibernate release notes:

http://www.hibernate.org/250.html?cmd=prntdoc

And I understand how the example in the release notes is illegal in that they are asking for instances of a joined class (i.e. b), and not instances of A who is the owner.

But, that's not the same thing I'm doing here. I'm asking for Asset to be joined with all of it's relationships, but only counting on the instances of Asset. So I'm asking for the equivalent of count(A) in the example so Asset is the owner. Why is that not valid?

I can think of several ways to fix this like dropping the joins, but what happens when I need a join to express something in the where clause? I'm just trying to count the maximum results that could occur from my query, but I'm trying to do it generically so I can write reusable pagination.

Thanks
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 3:15 am 
Newbie

Joined: Wed Jun 06, 2007 11:58 am
Posts: 13
I'm having a similar issue, any clue what's wrong ?

query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=v,role=null,tableName=public.file_visit_counter,tableAlias=filevisitc1_,origin=public.file file0_,colums={file0_.id ,className=com.gsi.core.domain.FileVisitCounter}}]
Code:
[select count(*) from com.gsi.core.domain.File file
         inner join fetch file.visits as v
         WHERE file.active=TRUE
         and file.status='A']


I also tried

Code:
[select count(file.id) from com.gsi.core.domain.File file
         inner join fetch file.visits as v
         WHERE file.active=TRUE
         and file.status='A']


I also tried

Code:
<one-to-one name="visits" class="com.gsi.core.domain.FileVisitCounter" property-ref="file" cascade="save-update" lazy="proxy"/>
[select count(file.*) from com.gsi.core.domain.File file
         inner join fetch file.visits as v
         WHERE file.active=TRUE
         and file.status='A']


The visits is defined in File as
Code:
<one-to-one name="visits" class="com.gsi.core.domain.FileVisitCounter" property-ref="file" cascade="save-update" lazy="proxy"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 7:06 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
Q-2 wrote:
I'm having a similar issue, any clue what's wrong ?


Yes drop the fetch joins from your query.

Quote:
query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=v,role=null,tableName=public.file_visit_counter,tableAlias=filevisitc1_,origin=public.file file0_,colums={file0_.id ,className=com.gsi.core.domain.FileVisitCounter}}]
Code:
[select count(*) from com.gsi.core.domain.File file
         inner join fetch file.visits as v
         WHERE file.active=TRUE
         and file.status='A']



Here you are fetch joining against file.visits, but you aren't using that in your where clause so as far as counting goes it shouldn't alter your
results to have them present or to take them away.

Quote:
I also tried

Code:
[select count(file.id) from com.gsi.core.domain.File file
         inner join fetch file.visits as v
         WHERE file.active=TRUE
         and file.status='A']


I also tried

Code:
<one-to-one name="visits" class="com.gsi.core.domain.FileVisitCounter" property-ref="file" cascade="save-update" lazy="proxy"/>
[select count(file.*) from com.gsi.core.domain.File file
         inner join fetch file.visits as v
         WHERE file.active=TRUE
         and file.status='A']


The visits is defined in File as
Code:
<one-to-one name="visits" class="com.gsi.core.domain.FileVisitCounter" property-ref="file" cascade="save-update" lazy="proxy"/>


In all of the above cases the joins are only present to eagerly load some relational object. I don't know why it works, but it does. Fetch joining in count queries doesn't seem to work.

Now if you need to load a relationship in order to query across it then you'd do an inner join file.visits so something like:

Code:
select count(*) from File file inner join file.visits as v where file.active=TRUE and v.count > 0


That works for me when I need something loaded so I can put it in my where clause.

Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 2:35 pm 
Newbie

Joined: Wed Jun 06, 2007 11:58 am
Posts: 13
Thanks a lot, that makes perfect sense.

The thing I try to achieve is a pagination thing. I use HibernatePage(search on the hibernate.org) to only get a window of the data. The problem with that is using scroll and last() on the HQL/SQL will take as much as extracting the data! So what I try to achieve here is to make the count as a HQL/SQL by striping dynamicaly the query and run it hoping that with will take lot less.

I have case where the last() takes 1.2sec - a lot indeed. Now same query with count(*) takes 21ms! My problem can also be the fact that I'm running Postgresql 7.4.7 but I'm upgrating to 8.2 so I hope that will speed up my queries.

My best - I'd have give you a credit for reply but I just realized that you answered the question by yourself - can you give yourself a credit :) ?

Q-2


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