-->
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.  [ 10 posts ] 
Author Message
 Post subject: parent with child collection isn't correctly refreshed
PostPosted: Fri May 19, 2006 4:49 am 
Newbie

Joined: Fri May 19, 2006 4:15 am
Posts: 3
edit: the topic of this thread was formerly "multiple sessions / refresh-problem"
but we realized, that using multiple session is not a cause for the problem.


NHibernate version: 1.0.2.0
Name and version of the database you are using: MS SQL Server 2000

Hello,

i've a problem with refreshing objets in a second session. There are two objects with a on-to-many-relation between them (the hbm-mapping-files are appended at the bottom).
In my application, i am using two sessions: One reader-session and one writer-session. The reader session is, like the name says, for reading data. There is only one reader session in the application. When i make changes to the entity objects, i am using a temporarly created session, the writer session, to persit the changes in the database.

This pattern is working well, to the point, where i have to refresh the parent-entity through the reader-session.
Here is the C# code snippet:
Code:

// gets the vather-object through the reader-session:
TestVater original = app.GetVater();
// creates a temporary writer-session:
NHibernate.ISession writerSession = m_SessionFactory.OpenSession();

// creats a 'working copy' of the object, which I want to edit:
TestVater workingCopy = writerSession.Get( original.GetType(), app.ReaderSession.GetIdentifier( original ) ) as TestVater;
workingCopy.Label = "test...";

// adds a child-entity
app.AddChild( workingCopy );

// saves the object through the writer session: (it does a Save() and a Flush())
// the changes a really saved to the database!
app.Save( writerSession, workingCopy );

// should refresh the original object through the reader session, but it doesnt work
app.Refresh( original );
// actually, it refreshes only the parent-object, exactly the "label"-property, but it doesnt refresh the
// child collection -> the newly added child is not there :(


But when I restart the application an load the parent object, the added child is there.
What should I do to bring Nhibernate to refresh the parent object AND its child-collection?

Thanks in advance,

regards kamil

------------------------

Mapping documents:
(VATER means vather, KIND means child in english)

Parent entity
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-cascade="none" default-access="field.pascalcase-m-underscore">
  <class name="Refresh.Test.Entities.TestVater,Refresh.Test" table="TEST_VATER" lazy="false">
   <id name="VId" column="V_ID" type="Int32">
      <generator class="native"/>
    </id>

    <property column="LABEL" type="String" name="Label" />

    <bag name="TestKinder" table="TEST_KIND" lazy="false" cascade="all-delete-orphan"
         outer-join="true" >
      <key column="V_ID"/>
      <one-to-many class="Refresh.Test.Entities.TestKind,Refresh.Test"/>
    </bag>

  </class>
</hibernate-mapping>


Child entity
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-cascade="none" default-access="field.pascalcase-m-underscore">
  <class name="Refresh.Test.Entities.TestKind,Refresh.Test" table="TEST_KIND" lazy="false">

      <id name="KId" column="K_ID" type="Int32">
      <generator class="increment"/>
    </id>
   
    <many-to-one name="VId" column="V_ID" class="Refresh.Test.Entities.TestVater,Refresh.Test" not-null="true" />
   
    <property column="LABEL" type="String" name="Label" />

   
  </class>
</hibernate-mapping>


Last edited by kamil on Tue May 23, 2006 11:41 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 5:44 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
post the stack trace, please.

Anyway, I don't think that your bi-session pattern is effective, among other things each session caches the objects it manipulates, also is resource intensive. Best practices sugest to keep a session open as minimum possible.


Cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:49 am 
Newbie

Joined: Fri May 19, 2006 4:15 am
Posts: 3
there is no stack trace beacause there is no exception.
while debugging, i step over
Code:
readerSession.Refresh( vatherObject );
and only
vatherObject.Label is being refreshed with the new value of the label-property ("test ..."), but the new child, which was added to the collection, and which is actually in the database is not there.

when i restart the my application, everything is ok.
or when i call first readerSession.Evict( vaterObject ) and after this readerSession.Get( ... ) to load the vaterObject again, i has its child-object loaded correctly.
but this is not a solution, because i lose the caching abillity of nhibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 9:02 am 
Newbie

Joined: Wed Mar 08, 2006 3:57 pm
Posts: 6
I am having a similar problem, but with only ONE session involved. Parent object members get refreshed but child collection members do not get refreshed. Is it just me, or is this an NHibernate bug? Does your problem go away if you only use a single session?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 11:34 am 
Newbie

Joined: Fri May 19, 2006 4:15 am
Posts: 3
taarheel, i've checked it with my application:
i get the same effects like you, if I use only one session:

I load a parent object, then I change manually in the database the data of the parent object (label) and I add a child for it.

when i refresh the parent object throug the same session I was loaded,
the parent object is being refreshed, but the new child is not there.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 1:41 pm 
Newbie

Joined: Wed Mar 08, 2006 3:57 pm
Posts: 6
Thanks, Kamil. I suspect this is an NHibernate bug. I'm going to post a question/note to the nhibernate-development listserv on this subject.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 4:16 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
That's right, Refresh essentially does Evict() and then Load(), so the child objects are not affected.

Hibernate 3 supports cascading Refresh. If you want that functionality, please open a JIRA issue.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 4:56 pm 
Newbie

Joined: Wed Mar 08, 2006 3:57 pm
Posts: 6
Thanks for the clarification, Sergey. IMHO, as a minimum, the Hibernate documentation could use some improvement in this area.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 12:27 pm 
Newbie

Joined: Wed Jul 04, 2007 5:31 pm
Posts: 17
Did anybody filed that in Jira?


Top
 Profile  
 
 Post subject: Re: parent with child collection isn't correctly refreshed
PostPosted: Wed Jul 08, 2009 1:41 am 
Newbie

Joined: Wed Jul 08, 2009 1:30 am
Posts: 1
I am facing the same problem. Can somebody help me?

How I can get Hibernate 3 ??


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