-->
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: Lazy initializing and collections...
PostPosted: Thu Jan 12, 2006 12:35 pm 
Newbie

Joined: Sun Jan 08, 2006 2:26 pm
Posts: 15
Hi there,

i am sitting here since hours and cant get this mapping fixed. I didnt find an solution in the forum search...

In my case i´ve got an Order which holds an ArrayList of OrdersDetails. OrdersDetails are holding an reference to an produkt. I use this mapping right now:

Code:
public class Order
   {
      private int id;
      
      private int orderNr;
      private ArrayList details;
      ...
      public Order() {}

      public IList Details
      {
         get
         {
            if ( details == null )
            {
               details = new ArrayList();
            }
            return details;
         }
         set
         {
            
         }
      }
      ...
      public override string ToString()
      {
         System.Text.StringBuilder sbuilder = new System.Text.StringBuilder();
         ...
         sbuilder.Append("\nDetails: [Quantity | price | productname]\n\t");
         foreach(OrdersDetails od in Details)
         {   
            sbuilder.Append("[");
            sbuilder.Append(od);
            sbuilder.Append("]\n\t");
         }
      
         return sbuilder.ToString();
      }
   }



Access is configured as properties. So i´ve set Details property as an IList instead of ArrayList and left the empty (is there the bug? but i dont know how to implement this otherwise)
Code:
public class Order
   {
      private int id;
      
      private int orderNr;
      private ArrayList details;
      public Order() {}

      public IList Details
      {
         get
         {
            if ( details == null )
            {
               details = new ArrayList();
            }
            return details;
         }
         set
         {
            
         }
      }
   }


And finally OrdersDetails

Code:
public class BestellungsDetails
   {
      
      private int id;
      private float articlePrice;
      
      private Product product;
      ...
   }



Saving is working fine and i got all the foreign Keys in the Table OrderDetails to Order. But they are not loaded... If i do for an example:

Code:
ICriteria criteria = session2.CreateCriteria(typeof(Order));
         IList commandes = criteria.List();
         
         
         foreach(Order o in commandes)
         {   
            
            Console.WriteLine(o.Details.Count);
            Console.WriteLine(o);
         }


All Orders are loaded probably, but no OrderDetails at all.
Is there any help? I am really confused and thin i wont get it work probably...

Regards

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 12:43 pm 
Newbie

Joined: Sun Jan 08, 2006 2:26 pm
Posts: 15
Damn editing, here are the mappings :-)

In the code above BestellungsDetails means OrderDetail *g*

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="NHTest.Order, NHTest" table="Order">
      <id name="Id" column="Id" type="Int32">
         <generator class="hilo" />
      </id>
      <property name="OrderNr" column="OrderNr" type="Int32"/>
      
      <list name="Details" table="OrderDetails" outer-join="true"  cascade="all-delete-orphan">
         <key column="OrderId"/>
         <index column="DetailNr"/>
         <composite-element class="NHTest.OrderDetails, NHTest">
            <property name="Quantitiy" type="Int32"/>
            <property name="Price" type="Single"/>
            <many-to-one name="Product" column="ProductId" outer-join="true" cascade="save-update"/>
            
         </composite-element>
      </list>
      <many-to-one
         name="...


And OrderDetails

Code:

Thx anyway

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 5:01 pm 
Newbie

Joined: Sun Jan 08, 2006 2:26 pm
Posts: 15
Is this just a Bug in HQL?

If i use session.Get

Code:
Order best
            = session2.Get(typeof(Order), 32769) as Order;
         Console.WriteLine(best.Details.Count);
         Console.WriteLine(best);


Everthing is working! Tested this althoug with a parent/child association...

Greets

Chris


Top
 Profile  
 
 Post subject: Try this out
PostPosted: Thu Jan 12, 2006 10:54 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
Set all the properties of the OrderDetails to be Virtual and then try again.


Top
 Profile  
 
 Post subject: Try this out
PostPosted: Thu Jan 12, 2006 10:59 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
Set all the properties of the OrderDetails to be Virtual and then try again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 5:15 am 
Newbie

Joined: Sun Jan 08, 2006 2:26 pm
Posts: 15
THANKS!

Thats working, i didnt see this in the documentation... :-(
So i am happy now *g*

Chris


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.