-->
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.  [ 9 posts ] 
Author Message
 Post subject: HQL inner join issue
PostPosted: Wed Mar 11, 2009 8:57 am 
Newbie

Joined: Fri Mar 06, 2009 7:13 am
Posts: 5
Hi,

The following hql throws an error of "could not resolve property: items of:"

string sql = "from Organization as o join fetch
o.Addresses a join fetch a.items x where x.Address1 like 'adr1'";
IQuery query = session.CreateQuery(sql);
return query.List<Organization>();

however if I construct the same query using ICreteria it works

ICriteria criteria = session.CreateCriteria(typeof
(Organization));
criteria.CreateAlias("Addresses", "alist");
criteria.CreateAlias("alist.items", "add");
criteria.Add(Expression.Like("add.State", "U%"));
return criteria.List<Organization>();

The mapping I am using is as follows

<class name="Organization" table="Organization" lazy="false">
<id name="OrganizationID" column="OrganizationID" type="int">
<generator class="native"/>
</id>
<property name="OrganizationName" column="OrganizationName"
type="String" not-null="true"></property>
<component name="Addresses">
<bag name="items" table="OrgAddressMap" lazy="false" cascade
="save-update" access="field">
<key column="OrganizationID"/>
<many-to-many class="Address" column="AddressID"/>
</bag>
</component>
</class>

What is wrong with the HQL statement?

Thanks

Anurag


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 7:14 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Using "fetch" in HQL tells hibernate to load the collection items as a join. You should not mix that with restrictions on the collection. I suppose that the query works fine if you remove both or at least the second "fetch".


From the doc:

Quote:
A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). Also, the associated objects are not returned directly in the query results. Instead, they may be accessed via the parent object.

It is possible to create a cartesian product by join fetching more than one collection in a query, so take care in this case. Join fetching multiple collection roles is also disabled for bag mappings. Note also that the fetch construct may not be used in queries called using Enumerable(). Finally, note that full join fetch and right join fetch are not meaningful.


from http://www.nhforge.org/doc/nh/en/index.html#queryhql-joins

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 12:34 pm 
Newbie

Joined: Fri Mar 06, 2009 7:13 am
Posts: 5
Hi,

I have tried removing the fetch as well but it still does not work. From the error message it seem that hql is unable to resolve the items alias, this rather surprising as ICreteria works perfectly.

Thanks

Anurag


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 1:19 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Sorry, missed that on the first look: Addresses is a component, so you don't need a join ! CreateAlias probably ignores that. Try

from Organization as o join fetch
o.Addresses.items x where x.Address1 like 'adr1'

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 7:14 am 
Newbie

Joined: Fri Mar 06, 2009 7:13 am
Posts: 5
Hi,

"from Organization as o join fetch
o.Addresses.items x where x.Address1 like 'adr1'"

worked perfectly. Thanks for the same. I had another question, if I remove "fetch", I get an error. Can you tell me why?

Thanks

Anurag


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 8:10 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What does the error/exception say ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 7:56 am 
Newbie

Joined: Fri Mar 06, 2009 7:13 am
Posts: 5
Hi,

Sorry for the late reply, the error message says "Could not execute query[SQL: SQL not available]", while the inner exception says

"The value "System.Object[]" is not of type "Organization" and cannot be used in this generic collection.
Parameter name: value"

Not sure what is going on. If I add the fetch everything works fine.

Thanks

Anurag


Last edited by avagarwal on Sun Aug 08, 2010 7:13 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 8:33 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Without the fetch hibernate returns an array of organization + addres per resulting line instead of putting the addresses in the collection. "fetch" tells hibernate to fill the collection.

"select o from Organization o ..." would always return organizations, regardless if you specify fetch or not.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 8:57 am 
Newbie

Joined: Fri Mar 06, 2009 7:13 am
Posts: 5
Hi,

Sorry for the late reply, the error message says "Could not execute query[SQL: SQL not available]", while the inner exception says

"The value "System.Object[]" is not of type and cannot be used in this generic collection.
Parameter name: value"

Not sure what is going on. If I add the fetch everything works fine.

Thanks

Anurag


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