-->
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.  [ 2 posts ] 
Author Message
 Post subject: subquery problem in hql, advice please
PostPosted: Tue Mar 15, 2005 4:34 pm 
Newbie

Joined: Tue Mar 08, 2005 5:05 pm
Posts: 9
Hi guys, I'm trying to do subselects and I'm a little confused/lost. The object has 2 sets, one is a set of contacts the other is a set of contact lists. A contact list is itself a set of contacts. What I want to do is provide a funciton that returns a set of contacts that is the union of those sets without any duplicates. In sql I'd do something like

select userid from user where userid in
(select userID from contactlistlink where listid in
(select listid from recommendationrecipientlink where recommendationID = ?))
or userid in
(select recipientid from recommendationrecipientlink where recommendationID = ?)

I've tried a few different HQL queries but can't seem to figure it out. Any help would be appreciated, thanks.

Hibernate version:
3.0rc1

Mapping documents: (extra stuff removed for readability)
top class has set of contacts and set of contact lists
<hibernate-mapping package="com.cascada.dataaccess">
<class name="Recommendation">
<!-- the set of contact lists being recommended to -->
<set name="contactLists" table="contactlist_recommendation_link">
<key column="recommendationID" not-null="true"/>
<many-to-many column="listID" class="ContactList"/>
</set>
<!-- the set of individual contacts being recommended to -->
<set name="contacts" table="contact_recommendation_link">
<key column="recommendationID" not-null="true"/>
<many-to-many column="contactID" class="Contact"/>
</set>
</class>
</hibernate-mapping>

the contact object
<hibernate-mapping package="com.cascada.dataaccess">
<class name="Contact">
<set name="contactLists" table="contactlist_contact_link">
<key column="contactID" not-null="true"/>
<many-to-many column="listID" class="ContactList"/>
</set>
</class>
</hibernate-mapping>

the contact list
<hibernate-mapping package="com.cascada.dataaccess">
<class name="ContactList">
<!-- each list is a set of contacts via a many to many relations-->
<set name="contacts" table="contactlist_contact_link">
<key column="listID" not-null="true"/>
<many-to-many column="contactID" class="Contact"/>
</set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
/**
* Retrieve a list of all unique contacts (flattens contacts + contact lists)
* @return
*/
public List getUniqueContacts() throws Exception
{
List results = null;
Session session = null;
try
{
session = HibernateUtil.currentSession();
String query =
"from Contact as ctc where ctc.id in (" +
"select contact.id from Recommendation as rec where rec.id=:id" +
") or ctc.id in (" +
"from ContactList as list where list.id in (" +
"select contactLists.id from Recommendation as rec where rec.id=:id" +
")" +
")";
Query q = session.createQuery(query);
q.setLong("id",this.getId().longValue());
results = q.list();
}
catch (Exception ex)
{
throw new Exception(ex);
}
finally
{
}
return results;
}

Full stack trace of any exception that occurs:
error found: java.lang.Exception: org.hibernate.hql.ast.QuerySyntaxError: Invalid path: 'contact.id' [from com.cascada.dataaccess.Contact as ctc where ctc.id in (select contact.id from com.cascada.dataaccess.Recommendation as rec where rec.id=:id) or ctc.id in (from com.cascada.dataaccess.ContactList as list where list.id in (select contactLists.id from com.cascada.dataaccess.Recommendation as rec where rec.id=:id))]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 1:52 pm 
Newbie

Joined: Tue Mar 08, 2005 5:05 pm
Posts: 9
Shameless bump but does anyone have any idea how to merge two sets?


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