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: NHibernate behaviour - replacing object wiht new object.
PostPosted: Thu Jun 14, 2007 5:38 am 
Newbie

Joined: Thu May 31, 2007 10:51 am
Posts: 8
Location: Edinburgh, UK
Hi there,

I have been trying to understand how NHibernate will deal with my scenario and what my options are.

My Scenario:

I have an Order.
The order has a Buyer [Person].
The order has a Delivery.
The Delivery has a Recipient [Person].

If the Recipient is the same as the Buyer, there is one record in the database.
The buyer then phones up customer services and asks to change the Recipient.
Updating the Recipient also changes the Buyer's details.

What will NHibernate try to do if I set Delivery.Recipient = new Person();
and call SaveOrUpdate on the order?

This scenario is from legacy data, we are rewriting the application using NHibernate. Is it possible to recreate this scenario in NHibernate (ie one person record for both Objects) when creating the Order?

I am struggling to find my answers within the forum or documentation.

Best Regards


Top
 Profile  
 
 Post subject: Re: NHibernate behaviour - replacing object wiht new object.
PostPosted: Thu Jun 14, 2007 7:44 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
richardlennox wrote:

If the Recipient is the same as the Buyer, there is one record in the database.
The buyer then phones up customer services and asks to change the Recipient.
Updating the Recipient also changes the Buyer's details.

What will NHibernate try to do if I set Delivery.Recipient = new Person();
and call SaveOrUpdate on the order?


You lost me a bit here. It sounds like you are saying you want it to update the recipient when the buyer's details change? And your concern is that it will not happen through NHibernate? If this is the case then fear not, it's exactly what will happen. In your object graph there will be one person object which happens to be referenced in two places.

Let me know if I didn't make the right assumptions.

Assuming your second question is unrelated, you are talking about cascade options. If you set the cascade of the delivery on the order object to save-update and then set the cascade of the recipient on the delivery object to save-update calling SaveOrUpdate() on the Order would in fact persist the new person.


Top
 Profile  
 
 Post subject: NHibernate behaviour - replacing object with new object
PostPosted: Thu Jun 14, 2007 9:09 am 
Newbie

Joined: Thu May 31, 2007 10:51 am
Posts: 8
Location: Edinburgh, UK
I will try and explain clearer.

The Buyer and the Recipient being the same have only one record in the database. Changing the name of this Person here changes it for both as they are the same.

C.Services want to be able to add a Person record rather than update the Buyer's details.

setting the cascade to save-update works really well.

My second question was regarding whether NHibernate allows the creation of the Order with the same functionality:

ie

Code:
Order ord = new Order();
ord.Buyer = new Person();
order.Delivery = new Delivery("XYZ", ord.Buyer);


What happens when I call SaveOrUpdate() on this Order.

Ideally I want 1 record in the orders able, 1 in the deliveries table and 1 in the People table.

The other potential is:

Code:
Order ord = new Order();
ord.Buyer = new Person();
order.Delivery = new Delivery("XYZ", new Person());


In this case I want 1 record in the orders able, 1 in the deliveries table and 2 in the People table.

I am tying to learn how to use NHibernate correctly, and this sort of usage is not clear in the documentation to me.

Thanks for your help.


Top
 Profile  
 
 Post subject: Re: NHibernate behaviour - replacing object with new object
PostPosted: Thu Jun 14, 2007 9:59 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
richardlennox wrote:
My second question was regarding whether NHibernate allows the creation of the Order with the same functionality:

ie

Code:
Order ord = new Order();
ord.Buyer = new Person();
order.Delivery = new Delivery("XYZ", ord.Buyer);


What happens when I call SaveOrUpdate() on this Order.

Ideally I want 1 record in the orders able, 1 in the deliveries table and 1 in the People table.

The other potential is:

Code:
Order ord = new Order();
ord.Buyer = new Person();
order.Delivery = new Delivery("XYZ", new Person());


In this case I want 1 record in the orders able, 1 in the deliveries table and 2 in the People table.

I am tying to learn how to use NHibernate correctly, and this sort of usage is not clear in the documentation to me.

Thanks for your help.


Yes, you got it. With cascade as I mentioned that is exactly what would happen.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 10:15 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Sorry, missed the point.

_________________
WBR, Igor


Top
 Profile  
 
 Post subject: NHibernate behaviour - replacing object with new object
PostPosted: Thu Jun 14, 2007 10:20 am 
Newbie

Joined: Thu May 31, 2007 10:51 am
Posts: 8
Location: Edinburgh, UK
Hi IgorK,

Thanks for your reply.

I understand what is happening in the OO code an what would happens if I change the reference or simply change the object state. I am struggling with how NHibernate persists the former to the database.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 10:24 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Yes, after reading your examples carefully I see the problem is not in object stuff, but in NH stuff.
Cascading is powerfull, but also a risky thing - you have to know what does each of cascade setting lead to...

_________________
WBR, Igor


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.