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.  [ 7 posts ] 
Author Message
 Post subject: Unexpected row count: 0; expected: 1
PostPosted: Wed Jun 11, 2008 10:25 am 
Newbie

Joined: Wed Jun 11, 2008 10:06 am
Posts: 6
I'm trying a very simple parent/child pattern with NHibernate with no luck.

The parent class:

public virtual string Id
public virtual string Name
pubic virtual ISet<Child> Children

the child class:
public virtual string Id
public virtual string Name

Child class has two public constructors:
One parameterless doing nothing
One with one parameter, assigning the Id

the hbm.xml file for parent:
<class name="Parent" table="parents">
<id name="Id" column="id">
<generator class="uuid.string"/>
</id>

<set name="Children" lazy="true" cascade="all" inverse="true">
<key column="parent"/>
<one-to-many class="Child"/>
</set>

<property name="Name" column="name"/>

</class>

the hbm.xml file for child:
<class name="Child" table="children">
<id name="Id" column="id">
<generator class="assigned"/>
</id>

<property name="Name" column="name"/>

</class>

Parent's constructor creates the list:
Children = new HashedList<Child>()

and I use the following code to save it (after opening the session and transaction)

Parent p = new Parent();
Child m = new Child("child_id");
p.Children.Add(m);
session.Save(p);
tx.Commit();

After doing this, the commit throws the exception. I thought it should be very easy to do this, but surprisingly there are no fully working examples of NHibernate 1.2 with C# generics around. All samples are partial.



Hibernate version:
1.2.1

Full stack trace of any exception that occurs:
at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledUpdate.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()
at Onyaka.Website.Secure.Test.Page_Load(Object sender, EventArgs e) in C:\Projects\NET Projects\Onyaka\Web\Onyaka.Website\Secure\Test.aspx.cs:line 45
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Name and version of the database you are using:
MySQL 5.0


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 11:08 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You've marked the parent end of the association as inverse, but you don't have an association on the child end:

Code:
<set name="Children" lazy="true" cascade="all" inverse="true">
<key column="parent"/>
<one-to-many class="Child"/>
</set>


You need:

Code:
<many-to-one name="Parent" column="parent_id" not-null="true" />


on Child (and you have to set the parent on the child object). Or just drop inverse="true" on Parent.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 11:27 am 
Newbie

Joined: Wed Jun 11, 2008 10:06 am
Posts: 6
Same thing.

Tried:
- Removing inverse from the parent
- Adding a Parent property to Child, addin inverse and adding many-to-one relationship to Child

Same exception.

The example given in NHibernate documentation is fictional because:

1. Properies are not virtual.
2. Trying a genetic class (like IList) with set tag throws an exception, complaining about casting IGenericSet to IList. The tag should be changed to bag for it not to throw that exception.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 11:28 am 
Newbie

Joined: Wed Jun 11, 2008 10:06 am
Posts: 6
Same thing.

Tried:
- Removing inverse from the parent
- Adding a Parent property to Child, addin inverse and adding many-to-one relationship to Child

Same exception.

The example given in NHibernate documentation is fictional because:

1. Properies are not virtual.
2. Trying a genetic class (like IList) with set tag throws an exception, complaining about casting IGenericSet to IList. The tag should be changed to bag for it not to throw that exception.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 11:37 am 
Newbie

Joined: Wed Jun 11, 2008 10:06 am
Posts: 6
A break through!

Create a parent
Create a child
add the child to parent's list
Save the child
Save the parent

this works.

But only saving the parent doesn't save the child and therefore doesn't work.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 11:45 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look here http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html#example-parentchild-update. I suppose, hibernate can't figure out that you child is really new. Explicitly saving it is one of the possible solutions.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 12:09 pm 
Newbie

Joined: Wed Jun 11, 2008 10:06 am
Posts: 6
The only way to make it work with one Save(parent) is to use SaveOrUpdateCopy(parent)

All other types of save (Save or SaveOrUpdate) throw the same exception.

Does this give anyone any more clues as to why this is happening?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.