-->
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.  [ 5 posts ] 
Author Message
 Post subject: help with collections
PostPosted: Mon Jan 12, 2009 7:13 am 
Newbie

Joined: Mon Jan 12, 2009 6:51 am
Posts: 2
Hi all,
I want to replace a collection from child with a new one. I try this code without sucess.
Any suggest are welcome.

_parents = _session.CreateCriteria(typeof(Parent)).List<Parent>();
foreach(Parent p in _parents) {
p.Children.Clear();
}

Child c = new Child();
c.Value = 500;

// add a child with a value of 500 to each parent

foreach(Parent p in _parents) {
_session.Transaction.Begin();
p.AddChild(c);
_session.Update(p);
_session.Transaction.Commit();
}


parent:
...
<bag name="Children" inverse="true" cascade="all-delete-orphan">
<key column="parent_id"/>
<one-to-many class="Child"/>
</bag>
...

child:
...
<many-to-one name="Parent" column="parent_id" />
...

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 8:37 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Since you have a bidirectional association, you also have to "null" the parent property in the childs.

Something like

Code:
class Parent
{
...
   public void ClearChildren()
   {
        foreach( Child c in Children )
             c.Parent = null;
        Children.Clear();
   }
...
}

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: help with collections
PostPosted: Mon Jan 12, 2009 11:10 am 
Newbie

Joined: Mon Jan 12, 2009 6:51 am
Posts: 2
Thanks, thats true and was useful but is not the solution to the problem.
The result remains wrong: I get a unique child record pointing to the last parent in the parents list.

The error was to add a unique instance of child to the collection.
The solution is create one instance for each iteration.

Child c = new Child(); // <---- wrong
c.Value = 500; // <----- wrong

// add a child with a value of 500 to each parent

foreach(Parent p in _parents) {
_session.Transaction.Begin();

Child c = new Child(); // <---- correct
c.Value = 500; // <----- correct

p.AddChild(c);
_session.Update(p);
_session.Transaction.Commit();
}


Thanks anyway,
Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 11:12 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Yes, obviously ... but missed that, too ;-)

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 8:19 am 
Beginner
Beginner

Joined: Fri Sep 26, 2008 9:19 am
Posts: 20
Location: Belgium
i'm not having a bidirectional relation between my parent (Condition) and child (ConditionWord) and i'm having problems deleting childs out of my IList<ConditionWord>

Adding is not an issue. if i add a ConditionWords to my list, it will be saved in the ConditionWord table.
I can also get them out of the database with getting the Condition. The ConditionWords will be filled in.

BUT if i try to clear the list (condition.Words.Clear()) it's telling me that the IList is Read-only, and i cannot delete anything. the IList is declared by NHibernate as an Array.

Any idea why that's happening? (using NHibernate 2.0)



Condition class (parent)
Code:
  <bag name="Words" cascade="all-delete-orphan">
      <key column="ConditionID"/>
      <one-to-many class="ConditionWord"/>
    </bag>




Code:
        private IList<ConditionWord> words = new List<ConditionWord>();

       public IList<ConditionWord> Words
        {
            get { return words; }
            set { words = value; }
        }


ConditionWord class (child)

Code:
<class xmlns="urn:nhibernate-mapping-2.2" name="ConditionWord" schema="dbo" table="ConditionWords" dynamic-update ="true">
    <id name="Id" access="field.camelcase" unsaved-value="0">
      <column name="ID" sql-type="int" not-null="true" unique="true"/>
      <generator class="native"></generator>
    </id>
...


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