-->
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.  [ 5 posts ] 
Author Message
 Post subject: Saving ManyToMany
PostPosted: Sat May 03, 2008 7:23 am 
Beginner
Beginner

Joined: Thu Nov 29, 2007 4:36 am
Posts: 20
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" assembly="IWaiter.Model"  namespace="IWaiter.Model">
   <class name="CustomerOrder" table="CustomerOrder">
      <id name="Id" column="Id" type="Int32" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <property name="DateCreated" column="DateCreated" type="DateTime" not-null="true" />
      <many-to-one name="CustomerTable" column="CustomerTableId" not-null="true" />
      <list name="LineItems" table="LineItem" lazy="true"  >
         <key column="OrderId"/>
         <index column="LineNumber"/>
         <composite-element class="LineItem" >
            <property name="Quantity" column="Quantity" type="Int32" not-null="true"/>
            <many-to-one name="Product" column="ProductId" not-null="true"/>
         </composite-element>
      </list>
   </class>
</hibernate-mapping>


This is the mapping of CustomerOrder. The problem lies with the LineItems collection.

If I load an order from the database, the collection gets populated correctly. If I try to save an order .... that's where the problem appears. The order is saved but the collection is not saved.


Code:
CustomerOrder order = new CustomerOrder ( );
         order.DateCreated = DateTime.Now;
         order.CustomerTable = new CustomerTableDao ( ).GetById ( 1, false );

         LineItem lineItem = new LineItem ( );
         lineItem.Product = new ProductDao ( ).GetById ( 6, false );
         lineItem.Quantity = 3;
         order.LineItems = new List<LineItem> ( );
         order.LineItems.Add ( lineItem );

         new CustomerOrderDao ( new CustomerTableDao ( ) ).Save ( order );
         new CustomerOrderDao ( new CustomerTableDao ( ) ).NHSession.Clear ( );
         order = new CustomerOrderDao ( new CustomerTableDao ( ) ).GetById ( order.Id, false );

Assert.IsTrue ( order.LineItems.Count > 0 );


Any thoughts ?


Top
 Profile  
 
 Post subject: Saving ManyToMany
PostPosted: Sat May 03, 2008 8:21 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I think you need to define the 'cascade' option on the list element:

Code:
<list name="LineItems" table="LineItem" lazy="true" cascade="all" >


Or you could use cascade="all-delete-orphan" if you want removed elements to be deleted too.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 03, 2008 2:06 pm 
Beginner
Beginner

Joined: Thu Nov 29, 2007 4:36 am
Posts: 20
That's not working ... tried it with all cascades.
With all-delete-orphan it throws :

NHibernate.MappingException: collection was not an association: CustomerOrder.LineItems


Top
 Profile  
 
 Post subject: Saving ManyToMany
PostPosted: Sun May 04, 2008 7:28 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I tried an example on my machine; you're right, no 'cascade=' is required. On mine, however, it works just fine.

I think the problem you're seeing is that you are clearing the session before the line-items have been written.

Take out the cascade option, and instead try:
Code:
new CustomerOrderDao ( new CustomerTableDao ( ) ).Save ( order );
new CustomerOrderDao ( new CustomerTableDao ( ) ).NHSession.Flush ( );
new CustomerOrderDao ( new CustomerTableDao ( ) ).NHSession.Clear ( );


Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 1:01 am 
Beginner
Beginner

Joined: Thu Nov 29, 2007 4:36 am
Posts: 20
OMG, i can't believe I forgot the flush .... damn, that's exactly the kind of error I see in other people's code .

Thank you so much for testing the code ... I wouldn't of seen it for the life of me.

Again, thank you.


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