-->
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.  [ 3 posts ] 
Author Message
 Post subject: not cascading as I wish...
PostPosted: Mon Feb 18, 2008 11:18 am 
Newbie

Joined: Tue Sep 25, 2007 6:43 am
Posts: 16
Hello,

I am doing all what is right, to my knowledge, but it seems that there is some problem in saving concerning cascades (1.2.1GA)

This test

Code:
public void ShouldPersistOrderWithoutExplicitlyCallingSaveOnOrder()
    {
        User u = new User();
        Order o = new Order();
        u.AddOrder(o); // bidirectional, so this takes care of giving o to
u and u to o
        Factory.GetUserDao().SaveOrUpdate(u); // <-- exception thrown here
      }

throws a NHibernate.PropertyValueException

Message:not-null property references a null or transient value: Client.Businesslayer.Order.User
Source:NHibernate
PersistentClass:Client.Businesslayer.Order
PropertyName:User
TargetSite:Void CheckNullability(System.Object[], NHibernate.Persister.Entity.IEntityPersister, Boolean)

StackTrace:

at NHibernate.Impl.SessionImpl.CheckNullability(Object[] values, IEntityPersister persister, Boolean isUpdate) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 1406
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 954
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 872
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 767
at NHibernate.Impl.SessionImpl.Save(Object obj) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 708
at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 1543
at NHibernate.Engine.Cascades.CascadingAction.ActionSaveUpdateClass.Cascade(ISessionImplementor session, Object child, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Engine\Cascades.cs:line 172
at NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, Object child, IType type, CascadingAction action, CascadeStyle style, CascadePoint cascadeTo, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Engine\Cascades.cs:line 562
at NHibernate.Engine.Cascades.CascadeCollection(CascadingAction action, CascadeStyle style, CollectionType collectionType, IType elemType, Object child, CascadePoint cascadeVia, ISessionImplementor session, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Engine\Cascades.cs:line 682
at NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, Object child, IType type, CascadingAction action, CascadeStyle style, CascadePoint cascadeTo, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Engine\Cascades.cs:line 582
at NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, IEntityPersister persister, Object parent, CascadingAction action, CascadePoint cascadeTo, Object anything) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Engine\Cascades.cs:line 643
at NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, IEntityPersister persister, Object parent, CascadingAction action, CascadePoint cascadeTo) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Engine\Cascades.cs:line 615
at NHibernate.Impl.SessionImpl.PreFlushEntities() in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 3606
at NHibernate.Impl.SessionImpl.FlushEverything() in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 3067
at NHibernate.Impl.SessionImpl.AutoFlushIfRequired(ISet querySpaces) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 2234
at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 4950
at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria) in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\SessionImpl.cs:line 4906
at NHibernate.Impl.CriteriaImpl.List() in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\CriteriaImpl.cs:line 312
at NHibernate.Impl.CriteriaImpl.UniqueResult() in C:\Users\Jan\Documents\Visual Studio 2005\Projects\Library and Components\NHibernate\1.2.1.GA\NHibernate-1.2.1.GA-src\src\NHibernate\Impl\CriteriaImpl.cs:line 437
at Client.Data.UserDao.GetByEmail(String email) in C:\Users\Jan\Documents\Projects\Client\Sources\branches\1.5\Client.Data\UserDao.cs:line 20
at Client.Tests.Data.TestOrderDao.DeleteUser() in C:\Users\Jan\Documents\Projects\Client\Sources\branches\1.5\Client.Tests\Data\TestOrderDao.cs:line 45
at Client.Tests.Data.TestOrderDao.ShouldPersistAddressWithoutExplicitlyCallingSaveOnAddress() in C:\Users\Jan\Documents\Projects\Client\Sources\branches\1.5\Client.Tests\Data\TestOrderDao.cs:line 38

Code:
User.AddOrder(Order) is like that:

    public virtual void AddOrder(Order o)
    {
      if (orders == null)
             orders = new List<Order>();
       orders.Add(o);
       o.User = this;
    }


Mappings:
Code:
<class name="User"
                                table="[User]"
                                lazy="true">
               <id name="Id"
                               column="UserID">
                       <generator class="identity" />
               </id>
               <bag name="Orders"
                                table="Orders"
                                cascade="save-update"
                                inverse="true">
                       <key column="UserID"/>
                       <one-to-many class="Order"/>
               </bag>
       </class>

<class name="Order"
        table="Orders"
        lazy="true">
       <id name="Id"
               column="OrderID">
       <generator class="identity" />
       </id>
       <many-to-one name="User"
                        column="UserID"
                        not-null="true"
                        class="User"
                        cascade="save-update" />
</class>



What is weird is, that the User is actually persisted, so he cannot be transient eihter, at the moment, when the order is persisted.
The order's user of course is not null either.


I am completely lost,

anybody could give me a hand?

Jan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 12:39 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I'm not so sure the problem is the cascade as much as it is the not-null FK to User ( Orders.UserID ). Still, your parent-child looks right to handle that issue. Sometimes the identity generator can screw with things, since it wants to perform a Save() just to generate the ID value, which in this case is premature.

Try one thing (don't know if it will affect anything): remove the cascade on Order.User.

I have done this successfully with a standard P-C relationship. That cascade is the only different thing I see.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 12:51 pm 
Newbie

Joined: Tue Oct 24, 2006 2:51 pm
Posts: 19
Maybe i'm wrong, but i think if you set inverse="true" on the Order collection, then you cannot persist the User , the "normal" persistence responsability in your case in set to the many-to-one association on the order, so, saving the order is the right choice

Alternatively, i think you have to set inverse="true" on the many-to-one on the order and set inverse="false" on the orders collection


In this case, i think that using a bag is not the best solution, but should work

p.s. sorry if i got wrong on something!


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