Problem summary:
I have Media objects that have related CrewMemberRel objects. CrewMemberRel objects have a CrewRole type. I want to fetch the CrewMemberRel objects based on the CrewRole type, so the mapping documents have a
where clause to switch on the crew role type:
Code:
<set name="talentRels" where="crew_role_id = 1">
..
</set>
<set name="producerRels" where="crew_role_id = 2">
..
</set>
[full mapping docs below]
This works fine.
The problem is when I want to use an isEmpty Expression from the Criteria API. The HQL that results from that Criteria does not include the
where clause restriction in the
exists clause:
Code:
(not exists (select 1 from cmsuser.crewmember_role_media_rel where this_.id=media_id))
instead of what I am looking for:
Code:
(not exists (select 1 from cmsuser.crewmember_role_media_rel where this_.id=media_id and crew_role_id = 1))
Is there a better way to model my relationship to get this?
Thanks!
Dan
Java:
Code:
Criteria criteria = getSession().createCriteria(Media.class);
Disjunction or = Expression.disjunction();
or.add(Expression.isEmpty('producerRels'));
// more experssions
criteria.add(or);
criteria.list();
Hibernate version: 3.1.2 Mapping documents:
Code:
<class name="Media" table="media" where="visible=1">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<!-- talent -->
<set name="talentRels" cascade="save-update" where="crew_role_id = 1">
<key column="media_id"/>
<one-to-many class="CrewMemberRoleRel" />
</set>
<!-- producers -->
<set name="producerRels" cascade="save-update" where="crew_role_id = 2">
<key column="media_id"/>
<one-to-many class="CrewMemberRoleRel" />
</set>
</class>
<class name="CrewMemberRoleRel" table="crewmember_role_pod_rel">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<many-to-one name="crewMember" column="crew_member_id" class="CrewMember"/>
<many-to-one name="crewRole" column="crew_role_id" class="CrewRole"/>
<many-to-one name="media" column="media_id" class="Media"/>
</class>