views:

177

answers:

2

I have a 3 tables which are "News", "Tags", "News_Tags" with Many-To-Many relationship.

With the following mapping the "News" and "Tags" table have been updating, but "News_Tags" has not been updated.

Can anyone help me?

News:

<class name="News" table="News" lazy="false">
  <id name="NewsID">
    <generator class="identity" />
  </id>
  <property name="Title"/>
  <set 
    name="TagsList" 
    table="News_Tags" 
    inverse="true" 
    lazy="false" 
    cascade="save-update">

      <key column="NewsID" not-null="true" />
      <many-to-many class="Tag" column="TagID" />
  </set> 
</class>

Tags:

<class name="Tag" table="Tags" lazy="false">
  <id name="TagID">
    <generator class="identity" />
  </id>
  <property name="TagName"/>
  <property name="DateCreated"/>

  <set 
    name="NewsList"
    table="News_Tags"
    inverse="true"
    lazy="false"
    cascade="save-update">

      <key column="TagID" not-null="true" />
      <many-to-many class="News" column="NewsID" />
  </set>

</class>

News_Tags

<class name="NewsTags" table="News_Tags" lazy="false">
  <id name="NewsTagID">
    <generator class="identity" />
  </id>    
  <property name="TagID"/>
  <property name="NewsID"/>
</class>

many thanks

Daoming.

+1  A: 

There are some strange things in this mapping.

  • Both collections, TagsList and NewsList, are inverse. So NHibernate does not store them. Inverse means: "this information is already in another collection, so ignore this when storing". Put inverse on only one side of the bidirectional relation.
  • NewsTags is mapped as a class, even if it is not a class. It is just a table in the database, used to map a (bidirectional) many-to-many relation. Just remove this class mapping.
  • the cascade is set by both collections. I'm not sure if you want to create new tags if they are referenced by a News instance, but you most probably never want to create new News instances because they are referenced by some tag. I would remove it there.
Stefan Steinegger
Thank you very much. You have saved my time. Good lad.
Daoming Yang
+1  A: 

I agree with @Stefan, your mappings are not right. If I correctly understand there is a many-to-many relation between News and Tags, News_Tags is your junction/join table in which case you should not have to provide a mapping file.

Your mapping files for News and Tags should look after the junction/join table without you having to map it. Consider the mappings I have between User and Role which is many-to-many, hopefully this will provide some guidance.

User Mapping

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">

<class name="BM.BEAST.Core.Common.User, BM.BEAST.Core" table="[User]" lazy="false">
  <id name="ObjId" column="ObjId" type="Guid">
    <generator class="guid"/>
  </id>
  <property name="UserName" column="UserName" type="String" not-null="true"/>
  <property name="FirstName" column="FirstName" type="String" not-null="true"/>
  <property name="LastName" column="LastName" type="String" not-null="true"/>
  <property name="Initials" column="Initials" type="String" not-null="true"/>
  <property name="LastLoginDtm" column="LastLoginDtm" type="Timestamp"/>
  <property name="Disabled" column="Disabled" type="Boolean"/>
  <property name="OnLine" column="OnLine" type="Boolean"/>    
  <bag name="Roles" table="UserRole" lazy="true">
    <key column="UserObjId"/>
    <many-to-many class="BM.BEAST.Core.Common.Role, BM.BEAST.Core" column="RoleObjId"/>
  </bag>
  <bag name="Sessions" table="Session" lazy="true" cascade="all-delete-orphan">
    <key column="ActiveUser"/>
    <one-to-many class="BM.BEAST.Core.Common.Session, BM.BEAST.Core" not-   found="ignore"/>
  </bag>
</class>

Role Mapping

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">

  <class name="BM.BEAST.Core.Common.Role, BM.BEAST.Core" table="[Role]" lazy="false">
    <id name="ObjId" column="ObjId" type="Guid">
      <generator class="guid"/>
    </id>
    <property name="Code" column="Code" type="String" not-null="true"/>
    <property name="Name" column="Name" type="String" not-null="true"/>
    <property name="Sequence" column="Sequence" type="Int16" not-null="true"/>
  </class>

</hibernate-mapping>
David