How to define mapping without intermediate class PostTag
creation? I have three tables
t_post(id...)
t_tag(id, name)
t_post_tag(id,post_id, tag_id)
I want to have a collection with Tags in Post type classes:
class Post
{
public virtual IEnumerable<Tag> Tags{ get; set; }
}
public class Tag
{
}
mappings:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Sample.Core" namespace="Sample.Core.Domain.Model" xmlns="urn:nhibernate-mapping-2.2">
<class name="Post" table="t_post" lazy="true" >
<id name="Id" column="id" type="System.Int64" unsaved-value="-1" generator="identity">
</id>
...
<bag name="Tags" lazy="true" cascade="none" inverse="true">
<key column="post_id"/>
<one-to-many class="Tag" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Sample.Core" namespace="Sample.Core.Domain.Model" xmlns="urn:nhibernate-mapping-2.2">
<class name="Tag" table="t_tag" lazy="true" >
<id name="Id" column="id" type="System.Int64" unsaved-value="-1" generator="identity">
</id>
</class>
</hibernate-mapping>
To map pairing table, without explicit Entity representing it, we have to use
<many-to-many>
. Also attributetable="..."
must be present - to instruct NHibernate about that pairing table. (In case, that we assign Tags into Posts, we should not mark such mapping as inverse)6.3. Collections of Values and Many-To-Many Associations
6.8. Bidirectional Associations
23.2. Author/Work (contains full example)