'NHibernate MismatchedTreeNodeException

I have a DB handling app in which I came across a problem while updating many to many relation's objects. I have a table Devices and a table Admittances that are connected with one another with a third table Admittances_Devices. Every device can be admitted many times and every admittance can have multiple devices. I put my mappings and classes below

public class Device
    {
        public Device()
        {
            Admittances = new List<Admittance>();
        }
        public virtual int ID { get; set; }
        public virtual string SerialNumber { get; set; }
        public virtual Manufacturer Manufacturer { get; set; }
        public virtual Model Model { get; set; }
        public virtual Volume Volume { get; set; }
        public virtual Type Type { get; set; }
        public virtual string Distinguishing { get; set; }
        public virtual IList<Admittance> Admittances { get; set; }
    }

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MeraserwHub.DBClasses" assembly="MeraserwHub">
    <class name="Device" table="Devices">
        <id name="ID" column="DeviceID" >
            <generator class="identity" />
        </id>
        <property name="SerialNumber" column="SerialNumber" />
        <property name="Distinguishing" column="Distinguishing" />
    
        <many-to-one name="Manufacturer" lazy="proxy" unique="true" column="Manufacturer" not-found="ignore" cascade="all-delete-orphan"/>
        <many-to-one name="Model" lazy="proxy" unique="true" column="Model" not-found="ignore" cascade="all-delete-orphan"/>
        <many-to-one name="Volume" lazy="proxy" unique="true" column="Volume" not-found="ignore" cascade="all-delete-orphan"/>
        <many-to-one name="Type" lazy="proxy" unique="true" column="Type" not-found="ignore" cascade="all-delete-orphan"/>

        <bag name="Admittances" table="Admittances_Devices" inverse ="true" lazy="true" cascade="save-update">
            <key column="DeviceID"/>
            <many-to-many class="Admittance" column="AdmittanceID"/>
        </bag>
    </class>
</hibernate-mapping>

public class Admittance
    {
        public Admittance()
        {
            Devices = new List<Device>();
        }
        public virtual int ID { get; set; }
        public virtual string Number { get; set; }
        public virtual Client Client { get; set; }
        public virtual User User { get; set; }
        public virtual DateTime Date { get; set; }
        public virtual string Notes { get; set; }
        public virtual IList<Device> Devices { get; set; }
    }

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MeraserwHub.DBClasses" assembly="MeraserwHub">
    <class name="Admittance" table="Admittances">
        <id name="ID" column="AdmittanceID" >
            <generator class="native" />
        </id>
        
        <property name="Number" column="Number" />
        <property name="Date" column="Date" />
        <property name="Notes" column="Notes" />
        
        <many-to-one name="User" lazy="proxy" column="[User]" />
        <many-to-one name="Client" lazy="proxy" column="Client" />

        <bag name="Devices" table="Admittances_Devices" cascade="save-update" generic="true">
            <key column="AdmittanceID"/>
            <many-to-many class="Device" column="DeviceID"/>
        </bag>
    </class>
</hibernate-mapping>

When I want to update a device and admittance so I could add a record to each table I get an error. You can find the code of the methods and the error below.

Device device = menu.allDevicesList[cbx_serialNum.SelectedIndex];

device.Admittances.Add(toAddAdmittance); `toAddAdmittance is an object defined earlier`
toAddAdmittance.Devices.Add(device); 
dbConnection.UpdateDevice(device); `dbConnection is NHibernate class object`
dbConnection.UpdateAdmittance(toAddAdmittance);

   public void UpdateDevice(Device device) `the method for admittances looks exactly the same`
        {
            try
            {
                using (mySession.BeginTransaction())
                {
                    mySession.Query<Device>()
                      .Where(a => a.ID == device.ID)
                      .UpdateBuilder()
                      .Set(a => a.SerialNumber, device.SerialNumber)
                      .Set(a => a.Manufacturer, device.Manufacturer)
                      .Set(a => a.Model, device.Model)
                      .Set(a => a.Volume, device.Volume)
                      .Set(a => a.Type, device.Type)
                      .Set(a => a.Distinguishing, device.Distinguishing)
                      .Set(a => a.Admittances, device.Admittances)
                      .Update();

                    mySession.Transaction.Commit();
                }
                //MessageBox.Show("Dane urządzenia zostały zaktualizowane");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Error has an inner exception "MismatchedTreeNodeException(140!=3)" and the stack trace as below. For admittances I get the same error.


w NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException()
w NHibernate.ql.AsStŁ.ANT LR.Exec.BasicExecutor..ctor(lStatement
statement, IQueryable persister)
w
NHibernate.Hql.Ast.ANTLR.QueryTranslatorlmpl.BuildAppropriatestatem
entExecutor(lStatement statement)
w
NHibernate.Hql.Ast.ANTLR.QueryTranslatorlmpl.DoCompile(lDictionary2
replacements, Boolean shallow, 5tring collectionRolej
w
NHibernate.Hql.AsŁANTLR.ASTQueryTranslatorFactory.CreateQueryTransl
ators(IQueryExpression queryExpression, IASTNode ast, String
queryldentifier, String collectionRole, Boolean shallow, IDictionary2
filters, ISessionFactorylmplementorfactory]
w
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTransl
ators(IQueryExpression queryExpression, String collectionRole, Boolean
shallow, IDictionary2 filters, ISessionFactorylmplementorfactory]
w
NHibernate.Engine.Query,.QueryPlanCache.GetHOQLQueryPlan(IQueryExpr
ession queryExpression, Boolean shallow, IDictionary2 enabledFilters)
w
NHibernate.lmpl.Abstractsessionimpl.GetHOQLQuerPlan(IQueryBD<pressio
n queryExpression, Eeoolean shallow)
w NHibernate.lmpl.Abstractsessionimpl.CreateQuery(IQueryExpression
queryExpression)
w NHibernate.Linq.DefaultQueryProvider.ExecuteDml[T](QueryMode
queryMode, Expression expression]
w MeraserwHub.NHibernate.UpdateDevice(Device device) w
Ev Kodowanie CHepostMeraserwHubiMeraserwHubiyMeraserwHubiNH
ibernate.cs:wiersz 394

Please help guys, cause I've been struggling with this for some time already!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source