2

I have two classes:

 public class CarModel
    {
        public virtual int Id { get; set; }
        public virtual string model_name { get; set; }
    }

and

  public class Transport
    {
          public virtual int Id { get; set; } 
          public virtual string lic_plate { get; set; } 
          public virtual int model { get; set; } 
          public virtual string odometer { get; set; }
          public virtual string moto_val { get; set; } 
          public virtual Class.CarModel Modelis { get; set; }    
    }

And mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="web_nt" namespace="web_nt.Models">

  <class name="Transport" table="transport" dynamic-update="true" lazy="false">
    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>
    <property name="lic_plate" />
    <property name="model" />
    <property name="odometer" />
    <property name="moto_val" />
    <many-to-one name="Modelis" column="model"  cascade="none" class="web_nt.Models.Class.CarModel" lazy="false" /> 
  </class>

  <class name="web_nt.Models.Class.CarModel" table="car_model">
    <id name="Id" column="id" type="int">
      <generator class="native" />
    </id>
    <property name="model_name" />
  </class>

</hibernate-mapping>

And I get exception when I try to send values to database(In view it works perfectly): + $exception {"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"} System.Exception {System.ArgumentOutOfRangeException}

And I can't find what might be wrong here?

user1883184
  • 106
  • 2
  • 11

1 Answers1

5

The issue here is in a doubled column mapping:

<property name="model" />    
<many-to-one name="Modelis" column="model"  
     cascade="none" class="web_nt.Models.Class.CarModel" lazy="false" /> 

Both properties (valueType and Reference) are targeting same column. Which is possible but not for Write operations. We have to make one of them readonly using insert="false" and update="false"

<property name="model" insert="false" update="false" />    
<many-to-one name="Modelis" column="model"  
     cascade="none" class="web_nt.Models.Class.CarModel" lazy="false" /> 

So, now we do have access to both properties, mapped to same column, but the INSERT, UPDATE will target that column only once. Because that was the original issue here: ...ndex was out of range. Must be non-negative and less than the size of the collection...

Also check the similar issue: https://stackoverflow.com/a/24248912/1679310

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Cool! But If I use model to get data to Tranport column, I should use insert/update false to many-to-one option, right? – user1883184 Jun 18 '14 at 06:39
  • + $exception {"could not insert: [web_nt.Models.Transport#173][SQL: INSERT INTO transport (lic_plate, model, odometer, moto_val, body_nr, color, made_date, autoage, buy_date, sell_date, travel_page, owner, region, rajonas, Id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]"} System.Exception {NHibernate.Exceptions.GenericADOException} I get this now...(there is more values since I added them) – user1883184 Jun 18 '14 at 06:42
  • Please, extend the asnwer with complete errror. Do you know what? there is an issue if you use switched readonly mode, then I suggested. Because during the INSERT, if the writable is reference its id could be created later (IDENTITY) and NHibernate will care. But if we set the ID of transient entity (which is 0 usually) the INSERT will end up in constraint violations... – Radim Köhler Jun 18 '14 at 06:43
  • I tried both ways - you way and switching. And I wrote whole eexception I get – user1883184 Jun 18 '14 at 06:45
  • Wait, I cannot see the whole exception! Could you paste it into your question?!? it is important because it is somewhere **after** *?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]"} System.Exception {NHibernate.Exceptions.GenericADOException}...* – Radim Köhler Jun 18 '14 at 06:47
  • Oh I fixed this, it was all about foreign key with other column – user1883184 Jun 18 '14 at 06:49
  • It works now (I deleted and it works perfectly), thank you:) – user1883184 Jun 18 '14 at 06:53
  • Great to see that! Enjoy NHibernate... amazing tool ;) – Radim Köhler Jun 18 '14 at 06:54
  • Its truly amazing, just hard as if I just started learning it. :) – user1883184 Jun 18 '14 at 07:16