-->
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.  [ 3 posts ] 
Author Message
 Post subject: Apparent failure to load Children objects
PostPosted: Thu Nov 16, 2006 1:40 pm 
Newbie

Joined: Thu Nov 16, 2006 12:33 pm
Posts: 2
We are writing a test document for testing CRUD operations. We have a very simple, 2 level Parent and Child database. The CRUD operations all work for Parent. However, for Child objects we can Create and Delete, but when we try to Read or Update the Children we get the exception below.

Stepping through the code, NHibernate appears to retrieve the child objects from the database, but IEnumerator returns an empty collection to our 'Parent' Object.


Any help will be Appreciated.

- Phillip

Classes are:

namespace nHibernateRelationship
{
public class Child
{
private string _name;
private string _parentname;
private Parent _parent;

protected Child()
{
}
public Child(string name, string parentname)
{
_name = name;
_parentname = parentname;
}
public virtual string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public virtual string ParentName
{
get
{
return _parentname;
}
set
{
_parentname = value;
}
}
public virtual Parent Parent
{
get
{
return _parent;
}
set
{
_parent = value;
}
}
}
}

using System;
using System.Collections;
using System.Text;
using Iesi.Collections;

namespace nHibernateRelationship
{
public class Parent
{
private string _name;
private string _optional;
private IList _children;

public Parent()
{
_children = new ArrayList();
}
public Parent(string name)
{
_name = name;
_children = new ArrayList();
}
public virtual string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public virtual string Optional
{
get
{
return _optional;
}
set
{
_optional = value;
}
}
public virtual IList Children
{
get
{
return _children;
}
set
{
_children = value;
}
}

public virtual void AddChild(Child c)
{
c.Parent = this;
_children.Add(c);
}
}
}


Hibernate version:
1.2 Beta

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="nHibernateRelationship.Child, nHibernateRelationship" table="children">
<id name="Name" column="name" type="string">
<generator class="assigned" />
</id>
<many-to-one name="Parent" class="nHibernateRelationship.Parent, nHibernateRelationship" column="ParentName" not-null="true" cascade="all"/>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="nHibernateRelationship.Parent, nHibernateRelationship" table="parent">
<id name="Name" column="name" type="string">
<generator class="assigned" />
</id>
<property name="Optional" column="Optional" type="String" length="40"/>
<bag name="Children" table="children" inverse="true">
<key column="ParentName"/>
<one-to-many class="nHibernateRelationship.Child, nHibernateRelationship"/>
</bag>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

public void Open()
{
session = factory.OpenSession();
}
public void Close()
{
session.Close();
}
public void Read(string Parent)
{
Open();
//
// TEST to read all children
//
Parent p = (Parent)session.Load(typeof(Parent), Parent);

IEnumerator childEnumerator = p.Children.GetEnumerator();
childEnumerator.MoveNext();
Child testC = (Child)childEnumerator.Current;

Close();
}


Full stack trace of any exception that occurs:

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: Enumeration already finished.


mscorlib.dll!System.Collections.ArrayList.ArrayListEnumeratorSimple.Current.get() + 0x93 bytes
> nHibernateRelationship.exe!nHibernateRelationship.DBWork.Read(string Parent = "bob") Line 41 + 0x8 bytes C#
nHibernateRelationship.exe!nHibernateRelationship.Form1.DoDBWork() Line 46 + 0xf bytes C#
nHibernateRelationship.exe!nHibernateRelationship.Form1.Form1() Line 17 + 0x7 bytes C#
nHibernateRelationship.exe!nHibernateRelationship.Program.Main() Line 17 + 0x13 bytes C#
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x32 bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2b bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x3b bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x81 bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x40 bytes


Name and version of the database you are using:

SQL Server 2000


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 12:51 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Typically you'd put your cascade attribute on the collection, not the child object, so this:
Code:
<bag name="Children" table="children" inverse="true">
<key column="ParentName"/>
<one-to-many class="nHibernateRelationship.Child, nHibernateRelationship"/>
</bag>

becomes
Code:
<bag name="Children" table="children" inverse="true" cascade="all">
<key column="ParentName"/>
<one-to-many class="nHibernateRelationship.Child, nHibernateRelationship"/>
</bag>


That's the only thing I can see in your mappings that is off.
You can read more here at http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#example-parentchild-cascades

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject: Thanks for the Suggestion.
PostPosted: Fri Nov 17, 2006 6:17 am 
Newbie

Joined: Thu Nov 16, 2006 12:33 pm
Posts: 2
Thanks for the suggestion. We've tried it and it's made no difference I'm afraid. There must be something else we're doing wrong.

Just to Clarify, the Enumerator object is returning, but with a count of zero. This is the same with the cascade as after it.

Regards - Phillip


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