Hello,
I have 3 classes Person, Address and Country. Person has a reference to Address and Address has a reference to Country. Each class is mapped to a table: persons, addresses and countries.
The primary keys for addresses and countries are auto-increment integers generated by the MySQL server, and for countries is a string generated from code.
The persons table has a foreign key referencing the addresses table's id column, and the addresses table has a foreign key referencing the countries table's id column.
I have no problem creating an Address object and save it to db but when I try the same thing with Person I get the following error:
NHibernate.ADOException : could not insert: [DbPersistanceTest.DbData.Person#1070921195574][SQL: INSERT INTO persons (FirstName, LastName, AddressID, Id) VALUES (?, ?, ?, ?)]
----> MySql.Data.MySqlClient.MySqlException : Cannot add or update a child row: a foreign key constraint fails (`appointments_book/persons`, CONSTRAINT `FK_persons_AddressID` FOREIGN KEY (`AddressID`) REFERENCES `addresses` (`Id`))
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
D:\work\TestPrj\DbPersistanceTest\DbPersistanceTest\test\TestUtils.cs(21,0): at DbPersistanceTest.Test.DatabaseUtils.Save[I](ISession session, Object address)
D:\work\TestPrj\DbPersistanceTest\DbPersistanceTest\test\PersonTest.cs(153,0): at DbPersistanceTest.Test.PersonTest.TestDbPersistance()
I don't understand what I do wrong since the Address is working just fine
Here are the mapping xml docs for the three classes. I've removed the other properties from them, as they are not important:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="DbPersistanceTest.DbData.Person, DbPersistanceTest" table="persons">
<id name="Id">
<generator class="assigned" />
</id>
<many-to-one name="Address" column="AddressID" />
</class>
</hibernate-mapping>
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="DbPersistanceTest.DbData.Address, DbPersistanceTest" table="addresses">
<id name="Id">
<generator class="native" />
</id>
<many-to-one name="Country" column="CountryID" />
</class>
</hibernate-mapping>
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="DbPersistanceTest.DbData.Country, DbPersistanceTest" table="countries">
<id name="Id">
<generator class="native" />
</id>
<property name="Name" />
</class>
</hibernate-mapping>