I'm trying to use MySQL for my database backend. Now I got
Code:
TestCase 'Talisay.Commands.Tests.CreateTest.TestCreateCustomer'
failed: NHibernate.ADOException : could not insert: [Talisay.Core.Customer]
----> MySql.Data.MySqlClient.MySqlException : #23000Cannot add or update a child row: a foreign key constraint fails (`talisay/customers`, CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`Id`) REFERENCES `salesorders` (`CustomerId`))
at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledIdentityInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
D:\ian\projects\csharp\talisay\src\Talisay\Commands\Create.cs(31,0): at Talisay.Commands.Create.Execute()
D:\ian\projects\csharp\talisay\src\Talisay\Commands\Tests\CreateTest.cs(26,0): at Talisay.Commands.Tests.CreateTest.TestCreateCustomer()
--ADOException
at MySql.Data.MySqlClient.PacketReader.CheckForError()
at MySql.Data.MySqlClient.PacketReader.ReadHeader()
at MySql.Data.MySqlClient.PacketReader.OpenPacket()
at MySql.Data.MySqlClient.NativeDriver.ReadResult(Int64& affectedRows, Int64& lastInsertId)
at MySql.Data.MySqlClient.CommandResult.ReadNextResult(Boolean isFirst)
at MySql.Data.MySqlClient.NativeDriver.SendQuery(Byte[] bytes, Int32 length, Boolean consume)
at MySql.Data.MySqlClient.MySqlCommand.GetNextResultSet(MySqlDataReader reader)
at MySql.Data.MySqlClient.MySqlCommand.Consume()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
TestFixture failed:
0 passed, 1 failed, 0 skipped, took 4.30 seconds.
Here's my Customers table
Code:
CREATE TABLE `customers` (
`Id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`Id`),
CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`Id`) REFERENCES `salesorders` (`CustomerId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
And the mapping files
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Talisay.Core" assembly="Talisay">
<class name="Customer" table="Customers">
<id name="Id" column="Id" type="Int32">
<generator class="identity"/>
</id>
<property name="CustomerName" column="CustomerName"/>
<property name="Title" column="Title"/>
<component name="Name" class="Name">
<property name="LastName" column="LastName"/>
<property name="FirstName" column="FirstName"/>
<property name="MiddleName" column="MiddleName"/>
</component>
<bag name="Orders" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
<key column="Id"/>
<one-to-many class="SalesOrder"/>
</bag>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Talisay.Core" assembly="Talisay">
<class name="SalesOrder" table="SalesOrders">
<id name="Number" column="Number">
<generator class="identity"/>
</id>
<many-to-one name="Customer" column="CustomerId" class="Customer" cascade="none" />
</class>
</hibernate-mapping>
Any hint? I'm kinda lost. Thanks in advance!