Hi,
I am a new NHibernate user and I have a problem, when I want to insert a new object to the database.
This is what I am using:
NHibernate 2.0.1.GA
.NET 3.5 SP1
MySQL 5.1
I can say, that everything works fine with NHibernate, except one thing:
I can not insert/update objects, that have a many-to-one relation. Objects with other relations can be inserted/updated (but the example I created for this question does not contain any other relations than one many-to-one relation).
If there is minimum one many-to-one relation (or more than on) this Exception will be thrown:
"A first chance exception of type 'System.IndexOutOfRangeException' occurred in NHibernate.dll."
Here are the mapping-files (just two tables, called aaa_order and aaa_customer):
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="TestAssembly">
<class name="TestNamespace.aaa_customer,TestNamespace" table="aaa_customer" lazy="true">
<id name="ID" column="ID" type="string">
<generator class="assigned" />
</id>
<property type="string" length="255" name="Name" column="`Name`" />
<bag name ="Orders" table ="TestNamespace.aaa_order" cascade="all-delete-orphan" lazy="false">
<key column ="`Customer_ID`" />
<one-to-many class ="TestNamespace.aaa_order"/>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="TestAssembly">
<class name="TestNamespace.aaa_order,TestNamespace" table="aaa_order" lazy="true">
<id name="ID" column="ID" type="string">
<generator class="assigned" />
</id>
<property type="string" length="255" name="Name" column="`Name`" />
<property type="string" length="64" name="CustomerID" column="`Customer_ID`" />
<many-to-one name ="Customer" class="TestNamespace.aaa_customer" column ="`Customer_ID`" cascade ="all" />
</class>
</hibernate-mapping>
So I think, the database structure is clear, isn't it (a very simple one to n relation between table Customer and table Order)?
I tested a little bit (ok, not just a little bit...) and I found out, that I can insert new objects of "aaa_order" , if I comment out the many-to-one relation in the maaping-file like this:
Code:
<!--
<many-to-one name ="Customer" class="TestNamespace.aaa_customer" column ="`Customer_ID`" cascade ="all" />
-->
But this is not, what I want.
Can anyone help me, please?
Kind regards,
Stefan