Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need the entity type. Take a look at any-type mapping (it stores the type name in the database in the relation table, but you don't need it in the entity model).</p> <p>See <a href="http://ayende.com/Blog/archive/2009/04/22/nhibernate-mapping-ltmany-to-anygt.aspx" rel="nofollow noreferrer">this blog post by ayende</a>.</p> <hr> <p><strong>Edit:</strong> tried to write an example.</p> <p>You could have an own table for each tagged object, this is easy and straight forward, you don't even need any types:</p> <pre><code>&lt;class name="Tag"&gt; &lt;!-- ... --&gt; &lt;property name="Name"/&gt; &lt;/class&gt; &lt;class name="Image"&gt; &lt;!-- ... --&gt; &lt;bag name="Tags" table="Image_Tags"&gt; &lt;key column="Image_FK"/&gt; &lt;many-to-many class="Tag" column="TagId "/&gt; &lt;/bag&gt; &lt;/class&gt; </code></pre> <p>Tried to use some advanced features to map it into a single table, but I think it doesn't work this way:</p> <pre><code>&lt;class name="Tag"&gt; &lt;!-- ... --&gt; &lt;property name="Name"/&gt; &lt;bag name="Objects" table="tblTagEntity" access="noop"&gt; &lt;key column="TagId"/&gt; &lt;many-to-any id-type="System.Int64" meta-type="System.String"&gt; &lt;meta-value value="IMAGE" class="Image"/&gt; &lt;meta-value value="BLOG" class="Blog"/&gt; &lt;column name="EntityType"/&gt; &lt;column name="EntityId"/&gt; &lt;/many-to-any&gt; &lt;/bag&gt; &lt;/class&gt; &lt;class name="Image"&gt; &lt;!-- ... --&gt; &lt;bag name="Tags" table="tblTagEntity" where="EntityType='IMAGE'"&gt; &lt;key column="EntityId"/&gt; &lt;many-to-many class="Tag" column="TagId "/&gt; &lt;/bag&gt; &lt;/class&gt; </code></pre> <p>The tricks here are:</p> <ul> <li><code>access="noop"</code> to specify the foreign key without having a property in the entity model, <a href="http://ayende.com/Blog/archive/2009/06/10/nhibernate-ndash-query-only-properties.aspx" rel="nofollow noreferrer">see this post</a>.</li> <li><code>where="EntityType='IMAGE'"</code> to filter the loaded data. </li> </ul> <p>The problem is that most probably the EntityType is not set to any useful value. This could be fixed somewhere, but I don't think that it is worth the effort.</p> <p>Someone else has probably a better idea.</p> <hr> <p><strong>Edit 2: another (working) solution</strong></p> <p>make the association table an entity:</p> <p>in short: </p> <ul> <li>Tag => TagEntity: not mapped or one-to-many inverse (noop)</li> <li>TagEntity => Tag: many-to-one</li> <li>TagEntity => Object: any</li> <li>Object => TagEntity: one-to-many inverse</li> </ul> <p>This should work straight forward.</p> <p>classes:</p> <pre><code>class Tag { string Name { get; set; } } class TagEntity { Tag Tag { get; set; } object Entity { get; set; } } class Image { IList&lt;TagEntity&gt; tags { get; private set; } } </code></pre> <p>The only drawback seems to be that you have to make sure that the bidirectional associations are consistent without loading to much data. Note that inverse collections are not stored.</p> <hr> <p><strong>Edit 2: Performance notes</strong></p> <p>When you add / remove tags, you could do a trick. TagEntity has a reference to the tagged entity. The Entity also has a list of TagEntities, but this is marked as inverse. (This means, they are loaded, but not stored.)</p> <p>You can add and remove tags without loading the Entity an without loading all the tags.</p> <p>Adding:</p> <ul> <li>Get Tag to add (or load proxy if you have the id of the tag)</li> <li>Load Entity (just proxy, using session.Load, no db access here)</li> <li>create new TagEntity, assign tag and entity-proxy</li> <li>save TagEntity</li> </ul> <p>Removing:</p> <ul> <li>Get TagEntity to remove</li> <li>delete TagEntity.</li> </ul> <p>Within the session, you don't have this tag assigned to/removed from the TagEntity. This works fine assumed that you only add or remove tags within this transaction.</p> <p>I you define a list of TagEntities on the Tag, you can do the same, without loading all the TagEntities just to add or remove one.</p>
 

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