-->
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: How to access items in a ISet
PostPosted: Tue May 24, 2005 10:06 am 
Newbie

Joined: Sun May 15, 2005 11:39 pm
Posts: 17
Location: Australia
Hi all,

Sorry if the answer for this is right in front of my face...

Could someone please tell me, with an ISet, how do I

a/ Look up an item based on the 'key' ... eg mySet["123"] ?
b/ Look up an item based on index ... eg mySet[0] ?

Thanks very much in advance,


DS


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 1:45 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
There's no way to do this. Sets are unordered, so they only support enumeration and Contains(). You probably want a map or a list (bag).


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 6:34 pm 
Newbie

Joined: Sun May 15, 2005 11:39 pm
Posts: 17
Location: Australia
Thanks very much Sergey. I think that I'm finally getting the hang of the different collections.

would you mind clarifying one thing for me? I wish to have a collection that has a stringed key on it (for example, an order my have detail lines marked "a","b","c"). I then wish to be able to say MyOrder.Details("a").value = ...

My understanding is that, to do this, I need to use an IDictionary, that is a <map> in NH.

the problem that I have is that I need to set up a bi-directional relationship from the order detail back to the parent... In my notes I found the following (not sure where I got it from)

Quote:
Please note that NHibernate does not support bidirectional one-to-many associations with an indexed collection (list, map, or array) as the "many" end, you have to use a set or bag mapping.


Given this constraint, is there any way for me to achieve the behaviour that I want? I'm (very!) aware that at this stage I'm not too familiar with the inner workings of NH, however I was very surprised to read of this limitation... I assume that there's good reason?

Thanks very much for your help (again!)


Damien


Top
 Profile  
 
 Post subject: Ordered collections
PostPosted: Sun May 29, 2005 12:19 pm 
I was wondering the same thing when I read about that limitation (4.8. Bidirectional Associations) in the NHibernate documentation.
However, it seems that the hibernate 2.03 documentation seems to suggest that you can have ordered collections on either one-to-many or many-to-many associations by specifying an index column (5.9. Bidirectional Associations).

I have an app in which the order of items in the collection is critical.
I am currently experimenting wih the methods explained in the Hibernate 2.03 documentation. I will post my results here.


Top
  
 
 Post subject:
PostPosted: Tue May 31, 2005 1:14 am 
Newbie

Joined: Sun May 15, 2005 11:39 pm
Posts: 17
Location: Australia
If anyone's interested, I've addressed this issue by creating 'collection wrappers' for each of IList, IDictionary and ISet collections. On instantiation, I basically copy the items into a hastable. Note that I've created an Interface iOPWObject_NHLoaded which contains a "KEY" property. This is applied to every object in my application that is loaded into a collection. Basiclly, every object 'must' expose it's own unique key, whatever that might be...

Hope this helps someone... it seems to be working well for me. Notice that the class is abstract. I derive my own collection wrappers from one of my three 'collection' wrappers depending on me wanting a Map, List or Set. I'm instantiating 100's of 1000's of objects in my app and performance doesn't seem too bad...

DS


Code:
using System;
using System.Collections;

namespace OPWLibrary
{
   /// <summary>
   /// A NHibernate.Collection.Bag is basically an Arraylist. You can iterate
   /// through it and can access items by index. There is however no key on
   /// it. We are forced by NH to use them because only Bags and Sets can
   /// be used in bidirectional relationships - ie relationships where the
   /// child can see the parent. This class will provide a wrapper for our
   /// Bags. Part of this will give a method to find a record by the key
   /// </summary>
   abstract public class clsGenericBagWrapper : ArrayList, iOPWObject, iOPWCollection
   {
      #region Private Variables
         private Hashtable   _HashedCollection;
      #endregion
      public clsGenericBagWrapper(IList Listn) : base(List)
      {
         _application = Application;
         LoadHashedCollection();
         SaveChecksumState();
      }

      /// <summary>
      /// So that we can look up these items by their key, load them into a hashtable
      /// using their key
      /// </summary>
      /// <param name="ChildType"></param>
      private void LoadHashedCollection()
      {
         _HashedCollection = new Hashtable();
         foreach (iOPWObject_NHLoaded o in this)
         {
            _HashedCollection.Add(o.Key,o);
         }
      }


      #region iOPWCollection Members

      /// <summary>
      /// One limitation of NH is that you can't use an IDictionary in
      /// a bidirectional relationship. You have to use a 'bag' or a 'set'.
      /// This sort of sucks, because, if you want to look up an item by the
      /// key, Going to try copying them into a hashtable to see if that helps..
      /// </summary>
      virtual public object ItemByKey(object key)
      {
            return this._HashedCollection[key];
      }

      #endregion
   }
}




I made three collection 'wrapper' base classes. For each of IList (Bag, List), IDictionary(ISet) and IMap (Map).
I put this interface on them all..

Code:
namespace OPWLibrary
{
   /// <summary>
   /// Interface for each object that we use as a collection or
   /// collection base
   /// </summary>
   public interface iOPWCollection
   {
      object ItemByKey(object key);
   }
}


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.