c# - NHibernate map one-to-many relationship with intermediate table -


how define mapping without intermediate class posttag creation? have 3 tables

t_post(id...) t_tag(id, name) t_post_tag(id,post_id, tag_id) 

i want have collection 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, have use <many-to-many>. attribute table="..." must present - instruct nhibernate pairing table. (in case, assign tags posts, should not mark such mapping inverse)

<bag name="tags" table="t_post_tag"     lazy="true" cascade="none" inverse="true">   <key column="post_id"/>   <!--<one-to-many class="tag" />-->   <many-to-many class="tag" column="tag_id"/> </bag> 

6.3. collections of values , many-to-many associations

a collection of entities own table corresponds relational notion of many-to-many association. many many association natural mapping of .net collection not best relational model.

<many-to-many     column="column_name"                               (1)     class="classname"                                  (2)     fetch="join|select"                                (3)     not-found="ignore|exception"                       (4) /> 

(1) column (required): name of element foreign key column.
(2) class (required): name of associated class.
(3) fetch (optional, defaults join): enables outer-join or sequential select fetching association. special case; full eager fetching (in single select) of entity , many-to-many relationships other entities, enable join fetching not of collection itself, attribute on <many-to-many> nested element.
(4) not-found (optional - defaults exception): specifies how foreign keys reference missing rows handled: ignore treat missing row null association.

6.8. bidirectional associations

a bidirectional association allows navigation both "ends" of association. 2 kinds of bidirectional association supported:

  • one-to-many set or bag valued @ 1 end, single-valued @ other
  • many-to-many set or bag valued @ both ends

23.2. author/work (contains full example)


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -