Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate: How to filter mapped collection based on discriminator type
    primarykey
    data
    text
    <p><strong>Info:</strong> There is a group object. Users can be invited to groups. There are two types of group invitations: external and internal. All invites are stored in single table using discriminator. The group object knows only about internal invites. </p> <p><strong>Problem</strong> In the Invite table there are two records. One for internal and one for external. If I load the group I will see both invites even the collection is of type InternalInvite. <img src="https://i.stack.imgur.com/u6jOz.png" alt="InternalInvite result">.</p> <pre><code>SELECT internalin0_.Group_id as Group3_1_, internalin0_.Id as Id1_, internalin0_.Id as Id0_0_, internalin0_.Group_id as Group3_0_0_, internalin0_.User_id as User5_0_0_ </code></pre> <p>FROM [AbstractInvite] internalin0_ WHERE internalin0_.Group_id = 'be60b160-659e-4157-b033-9f9e00e346c7'</p> <p>When I change the ISet collection type to AbstractInvite it will load all invites with the correct type.</p> <p><strong>Question</strong> How can I map the collection property to only InternalInvites?</p> <p>I will try to minimize the code as much as I can.</p> <p><strong>The group and the group mapping:</strong></p> <pre><code>public class Group { public Group() { InternalInvitations = new HashedSet&lt;InternalInvite&gt;(); } public virtual Guid Id { get; set; } public virtual ISet&lt;InternalInvite&gt; InternalInvitations { get; set; } } public class GroupMap : ClassMap&lt;Group&gt; { public GroupMap() { Id(x =&gt; x.Id); HasMany(x =&gt; x.InternalInvitations).Inverse(); } } </code></pre> <p><strong>The external and internal invite + mappings:</strong></p> <pre><code>public abstract class AbstractInvite { public virtual Guid Id { get; set; } public virtual Group Group { get; set; } } public class ExternalInvite : AbstractInvite { public virtual string Name { get; set; } } public class InternalInvite : AbstractInvite { public virtual User User { get; set; } } public class AbstractInviteMap : ClassMap&lt;AbstractInvite&gt; { public AbstractInviteMap() { Id(x =&gt; x.Id); References(x =&gt; x.Group); DiscriminateSubClassesOnColumn("type"); } } public class ExternalInviteMap : SubclassMap&lt;ExternalInvite&gt; { public ExternalInviteMap() { Map(x =&gt; x.Name); } } public class InternalInviteMap : SubclassMap&lt;InternalInvite&gt; { public InternalInviteMap() { References(x =&gt; x.User); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload