These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: cannot save object having a many-to-one relation
PostPosted: Fri Sep 21, 2007 8:12 am 
Newbie

Joined: Fri Sep 21, 2007 7:56 am
Posts: 1
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>


Top
 Profile  
 
 Post subject: You are missing alias.
PostPosted: Wed Sep 26, 2007 2:49 am 
Newbie

Joined: Fri Jun 01, 2007 2:01 am
Posts: 3
Location: pakistan
hi,

You have to create alias of your entity and use it while specifying field names.

Example:

SELECT * FROM person P WHER P.id = 1 AND P.name = 'GA'

If hav'nt got my point, send me your query string. so that i would help u better.

Regards

_________________
Ghulam Abbas
Software Engineer
Pakistan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.