-->
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.  [ 4 posts ] 
Author Message
 Post subject: Persisting related objects
PostPosted: Wed Nov 29, 2006 11:46 am 
Newbie

Joined: Wed Mar 29, 2006 5:04 am
Posts: 9
Location: Antwerp, Belgium
I'm having a problem with the persistance of related objects. I'll show an example build on top of the Northwind-database.

//Sessionfactory already created
IList products = _session.CreateCriteria(typeof(Product)).List();

Product product = (Product) product[0];
product.Category.CategoryName = "Smosken";

_session.SaveOrUpdate(product);
_session.Flush();


If I run the above sample, NHibernate updates the category-table for me, as I expected. BUT : If I close and dispose the session after I get the Ilist of products, and then I reconnect product using _session.Lock(product,LockMode.None) (I recycle my _session-variable, but it is a brand new session) nothing happens, and the category-table stays unchanged. Why is that ? How can I make sure that the categories gets updates, without the need of looping through all related objects and bags myself ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 6:34 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
I am making a couple of assumptions here. I am assuming that you have both a Produt class and a Category class and that the Product class has a property that encapsulates a Category. I am also assuming that the CategoryName in the database never actually changes, just the CategoryName of the in-memory Category instance.

It looks like you are not saving the category object. You have the code:
Code:
_session.SaveOrUpdate(product);

This only saves the values contained in the Product you create, not the associated Category. Since you are not changing any of the Product values in the code provided, you should be able to change the above code to...
Code:
_session.SaveOrUpdate(product.Category);

...and the category name should be persisted to the database.

I am guessing what is happening is that the Category object with the changed name is only changed in the session. This means that the Category will apear to have it's name changed for every future read of that Category made from that session. This happens because the session is caching that instance and not reading from the database again. Once the session is disposed, that instance of the Category goes with it without ever being persisted to the database. So when a new session is created and the value is read from the database on the first read, you receive the old, unchanged database value.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 4:17 am 
Newbie

Joined: Wed Mar 29, 2006 5:04 am
Posts: 9
Location: Antwerp, Belgium
onevextchuck,

My example is based on the Northwind-db, so indeed, my Product-class has a property that encapsulates a Category (and the Category-class has a collection of Products).

I know that, when I use
[code]_session.SaveOrUpdate(product.Category);[/code]
The change in category gets persisted.
When I use
[code]_session.SaveOrUpdate(product);[/code]
it does not. However this is only true when executing the saveorupdate on [u]another[/u] session, than the one used to fetch the product ! When I execute the saveorupdate on the same session, my update on category gets persisted without any problem (I even ran tests by fetching an employee and then changing [code]MyEmployee.Orders[0].OrderDetails[0].Product.ProductName[/code]. When calling [code]_session.SaveOrUpdate(MyEmployee);[/code] my change in the productname gets persisted to the database)

So my question remains the same :
Is there anything I can do, to get the same behaviour when using the same or another session ? Or is this behaviour by design ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 3:27 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
Sorry for my confusion there.

I was able to duplicate the situation you are talking about. I then added a second call to ISession.Lock: session.Lock(product.Category, LockMode.None) and the SaveOrUpdate behaved as expected. It looks like the Lock call does not cascade.

If this is by design, I do not know. I imagine it may be difficult to determine if the session needs to reassociate child references without accessing those references causing them to load from the database. But that is only speculation.

Sorry I could not be of more help.


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