Hibernate version: 3.1.3
Name and version of the database you are using: MySQL 5.0
Mapping documents:
Code:
<class name="Document" table="documents">
<set name="authors" table="authors" cascade="all">
<key column="document_ID" not-null="true" />
<many-to-many column="person_ID" class="Person" />
</set>
<set name="editors" table="editors" cascade="all">
<key column="document_ID" not-null="true" />
<many-to-many column="person_ID" class="Person" />
</set>
<set name="publishers" table="publishers" cascade="all">
<key column="document_ID" not-null="true" />
<many-to-many column="person_ID" class="Person" />
</set>
</class>
I have a situation where my primary entity, Document, has several many-to-many relations to other tables (Document object contains several Sets: Authors, Publishers, Editors). Each of those Sets contains instances of a Person entity. Any Person can be in any of the three Sets of any Document.
Given a particular Person, I need a query that will return all Documents that have that Person in either the Authors, Publishers, or Editors sets.
I've tried the following, which does not work (it returns no results):
Code:
session.createCriteria(Document.class)
.createAlias("authors", "a")
.createAlias("editors", "e")
.createAlias("publishers", "p")
.add(Restrictions.disjunction()
.add(Restrictions.eq("a.id" idIAmLookingFor))
.add(Restrictions.eq("e.id" idIAmLookingFor))
.add(Restrictions.eq("p.id" idIAmLookingFor))
);
Interestingly, the following does work (if I only try to join to the Authors set instead of all three):
Code:
session.createCriteria(Document.class)
.createAlias("authors", "a")
.add(Restrictions.eq("a.id" idIAmLookingFor));
Here is the HQL that does what I want, but I need to find a solution using Criteria API so I can combine it with QBE:
Code:
from Document
where :personID in elements(authors)
or :personID in elements(editors)
or :personID in elements(publishers)
I've been through the reference docs several times, but can't seem to find an example that does this kind of joining via Criteria API.
What am I doing wrong? Or where in the docs can I find examples of this? I'm willing to do my own research, just need some guidance.
Thanks in advance,
Eric