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))]
|