0

I have a mapping and POCOs as below. The problem is that tbFNamesFeature is not being updated when this object is saved (whereas tblFeature is) I've tried different values for cascade, to no effect, so I'd have to say there's something I don't get. (see ??? in the xml).
What am I doing wrong?

tbFNamesFeature has 2 columns:

FNamesId (PK, int, not null)
FeatureId (PK, int, not null)

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Feature, Infrastructure.Interface"
         table="tblFeature">
    <id name="Id" type="Int32" unsaved-value="0">
      <column name="FeatureId" sql-type="int" not-null="true" unique="true" index="PK_tblFeature" />
      <generator class="native" />
    </id>
    <property name="Description" type="String">
      <column name="Description" length="100" sql-type="varchar" not-null="false" />
    </property>
    <bag name="FNames" table="tbFNamesFeature" inverse="true" lazy="false" cascade="???">
      <key>
        <column name="FeatureId" sql-type="int" not-null="true" />
      </key>
      <many-to-many class="FName, Infrastructure.Interface">
        <column name="FNamesId" sql-type="int" not-null="true" />
      </many-to-many>
    </bag>
  </class>
</hibernate-mapping>

Feature.cs

public partial class Feature : System.IComparable
{
        protected int id;
        protected string description;

        public virtual int Id
        {
            get { return this.id; }
            set { this.id = value; }
        }

        public virtual string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }
}

Feature.part.cs

public partial class Feature : System.IComparable
{
        private System.Collections.Generic.IList<FName> fnames;

        public virtual System.Collections.Generic.IList<FName> FNames
        {
            get
            {
                if (this.fnames == null)
                {
                    this.fnames = new System.Collections.Generic.List<FName>();
                }
                return this.fnames;
            }
            set {
                this.fnames = value;
            }
        }
 }
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139

1 Answers1

1

edited to reflect discussion: As tblFNamesFeature is a many-to-many table, the mapping needs to be set up with inverse="false" to indicate that other end the relationship is not responsible for saving the collection.

cascade="all" also needs to be set (which covers saves, updates, and deletes).

gusgorman
  • 170
  • 1
  • 11
  • Are you saying I need a mapping for `tbFNamesFeature `? – Al Lelopath Nov 05 '18 at 15:44
  • Yes. You need a mapping for every Entity/Table in your model. The only exception is pure many-to-many tables. – gusgorman Nov 05 '18 at 15:53
  • Actually, sorry I've just re-read your post. Is tbFNamesFeature supposed to be a many-to-many table? If so, what is it linking to? – gusgorman Nov 05 '18 at 15:59
  • Yes `tblFNamesFeature` is many-to-many. There is a table, `tblFNames`, which has `FNamesId` as it's PK (as does`tblFNamesFeature`). I have a mapping file for 'tblFNames'. – Al Lelopath Nov 05 '18 at 16:18
  • I think inverse="true" is saying that the other end of the relationship is responsible for the save. Try inverse="false". – gusgorman Nov 05 '18 at 16:33
  • This appears to be the solution, though let me test it some more. – Al Lelopath Nov 05 '18 at 17:52
  • The solution is inverse="true" AND cascade="all". If you will edit your answer to include this, I will check it off. – Al Lelopath Nov 07 '18 at 16:56