I'm trying to write an HQL query that needs to check for an empty
collection.
My query looks something like this:
Select a from Announcement a
inner fetch join a.Groups groups
join groups.UserInGroup uig
where
(
uig.Role = some elements(a.Group_Roles)
or a.Group_Roles is null <-- the problem is here
) and
uig.User.id = XX
Do give you a little info about what my domain looks like. I have
Announcements which can be "published" to groups. This is done using a
many-to-many mapping (via m2m table) to the Groups table. This m2m
mapping table also contains a field to restrict what role in the group
the announcement should be visiable to.
So my table are the following:
Announcements,
Announcment_Publications, <-- A many-to-many table that defines what
groups/group roles should see the announcement
Groups,
UsersInGroup
Users
In my query I want to check that the user's role in the group (defined
in the UsersInGroup table) matches the role defined in the m2m table
OR that not role is defined in the m2m table, just a group (this would
be a NULL value in the role field).
I've mapped the roles from the m2m table to the Announcement class
using the following mapping:
<bag name="Group_Roles" table="Announcement_Publications">
<key column="Anp_Announcement_id"/>
<element column="Anp_Group_Role" type="string"/>
</bag>
The groups are mapped like this:
<bag name="Groups" lazy="false" inverse="true" cascade="all"
table="Announcement_Publications">
<key column="Anp_Announcement_id" />
<many-to-many fetch="join" class="N.Group, N"
column="Anp_Group_id" />
</bag>
When I try something like the above query to check if the roles field
in the m2m table is null, I get this exception:
NHibernate.QueryException: unindexed collection before []
I hope I've provided enough information about my domain and mapping
for you to understand my setup.
How could I write the above query to check for "null" roles? Is my
mapping correct?