-->
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.  [ 6 posts ] 
Author Message
 Post subject: Cascading saves on new entities with new children
PostPosted: Fri Dec 07, 2007 12:51 pm 
Newbie

Joined: Thu Mar 01, 2007 1:37 pm
Posts: 7
I am extremely confused as to why this is failing, or why it should be failing. After much research I see that many others have these issues, yet very few answers.

Logically, I would feel that I should be able to do this: (Nhib 1.2.0)


assume session has been created, etc etc.

Code:
Orders o = new Orders();

OrderItem i = new OrderItem();
i.Order = o;
o.Items.Add(i);

session.SaveOrUpdate(o);
session.Flush();

No matter how I build my maps, changing cascade and inverse settings, I cannot get this to work without the transient object exception:
object references an unsaved transient instance - save the transient instance before flushing: BLL.Orders

For simplification, these would be the appropriate maps:

Code:
<class name="Orders" table="Orders">
    <id name="ID" column="ID" type="Int64" unsaved-value="0">
        <generator class="identity"/>
    </id>
    <bag name="Items" inverse="true" cascade="all-delete-orphan">
        <key column="OrdersID" />
        <one-to-many class="BLL.OrderItem, BLL" />
    </bag>
</class>

<class name="OrderItem" table="PublisherPublicationFileSlot">
    <id name="Id" column="ID" type="Int64" unsaved-value="0">
        <generator class="identity"/>
    </id>
    <many-to-one name="Order" column="OrdersID" class="BLL.Orders, BLL" />
</class>


What I am attempting to do is, from where I sit, a perfectly reasonable expectation. The only examples I can find that are similar to my example here, load an already persisted parent class, then add to the collection, and save the parent again.

Does NHib not support this seemingly obvious behavior?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 08, 2007 3:58 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
NHibernate does in fact support this feature. I am not sure that your mapping files are correct though.

One guess could be that it doesn't look like you are using fully qualified class names. Perhaps NHibernate thinks you are using a different class than what you mapped. All of my mapping files contain the full type name including assembly.

If you provided a mapping file which is not exactly what you used then maybe that is why it looks that way. Try using the full type name to see if that helps.

If that doesn't help then something else must be wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 9:44 am 
Newbie

Joined: Thu Mar 01, 2007 1:37 pm
Posts: 7
I do establish the full namespace and assembly names on the nhibernate-mapping level of the map. So that is not it. Besides, if it was a class issue, then I would guess it wouldnt be a transient instance error...

I could provide more info about the maps, but this basic map demonstrates the relationship that is causing the issue.


What further information is required?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 10:24 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Have you tried exactly what you posted? To me it looks like that should work. That's why I'm asking to see if the mappings you are having trouble with differ in any way from your posted example.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 10:29 am 
Newbie

Joined: Thu Mar 01, 2007 1:37 pm
Posts: 7
Yes, I have tried. I can also see what is happening, not why though.

After I get the exception and check the db, I can see that Orders has been created, and that OrderItem has been created, however it leaves the OrdersID field as null.

Based on that, it looks like NHib is saving the Orders object, then forgetting that the OrderItem is related to the saved order, and instead now related to a new transient Orders entity when it walks the relationship.

What would cause this to happen?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 11:32 am 
Newbie

Joined: Thu Mar 01, 2007 1:37 pm
Posts: 7
Nevermind, issue resolved.

My issue, with serialization and new instances, not nhib.

Remember kids, a *new* instance of your class, serialized, and deserialized, is not the same between serializations.


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