Hi there
how do I write a Hibernate query for the following situation?
I have a MailList object, which contains a Collection of Contact objects.
There is a many-to-many relation between MailLists and Contacts. So MailLists can contain 0 to many Contacts, and Contacts can be contained by 0 to many MailLists.
Now I want to find all the MailLists which contain some specific contacts. That is, I have a list/array of Contacts (actually only their ids) and I want to find all the MailLists which contain at least one of the Contacts.
A sort of pseudo HQL:
find MailList where maillist.contacts contains-at-least-one-of (contact_1, contact_2, ... )
Thanks,
Peter
Hibernate version:
2.1.6
Mapping documents:
<hibernate-mapping>
<class name="dk.peter.common.impl.AbstractBase" table="BASE" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<joined-subclass name="dk.peter.maillist.impl.MailListImpl" table="MAILLIST">
<key column="ID" />
<property name="name" column="NAME" type="string" />
<set name="contacts" table="MAILLIST_CONTACT" lazy="true">
<key column="MAILLIST_ID"/>
<many-to-many column="CONTACT_ID" class="dk.peter.party.impl.AbstractContact"/>
</set>
</joined-subclass>
<joined-subclass name="dk.peter.common.impl.AbstractNode" table="NODE">
<key column="ID" />
<set name="children" table="NODE">
<key column="PARENT_ID"/>
<one-to-many class="dk.peter.common.impl.AbstractNode"/>
</set>
<many-to-one name="parent" column="PARENT_ID" class="dk.peter.common.impl.AbstractNode" />
<joined-subclass name="dk.peter.party.impl.AbstractParty" table="PARTY">
<key column="ID" />
<joined-subclass name="dk.peter.party.impl.AbstractContact" table="CONTACT">
<key column="ID" />
<property name="number" column="NUMBER" type="integer" />
<property name="address" column="ADDRESS" type="string" />
<property name="telephone" column="TELEPHONE" type="string" />
<property name="telephoneMobile" column="TELEPHONE_MOBILE" type="string" />
<property name="emailAddress" column="EMAIL_ADDRESS" type="string" />
<property name="remark" column="REMARK" type="string" />
<joined-subclass name="dk.peter.party.impl.PersonImpl" table="PERSON">
<key column="ID" />
<property name="firstNames" column="FIRSTNAMES" type="string" />
<property name="lastName" column="LASTNAME" type="string" />
<property name="initials" column="INITIALS" type="string" />
</joined-subclass>
<joined-subclass name="dk.peter.party.impl.CompanyImpl" table="COMPANY">
<key column="ID" />
<property name="name" column="NAME" type="string" />
<property name="cvrNumber" column="CVR_NUMBER" type="string" />
</joined-subclass>
</joined-subclass>
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
Hypersonic
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|