-->
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: Arraylist?
PostPosted: Fri Feb 23, 2007 4:44 pm 
Newbie

Joined: Tue Feb 20, 2007 12:04 pm
Posts: 16
Does anyone know how to handle mapping for a class that extends ArrayList? I'm getting a cast exception when I try list or bag.

For example:

Code:
namespace com.test
{

   public struct Declarations
   {
      public const string SchemaVersion = "urn:com.test.schema";
   }


   [Serializable]
   [EditorBrowsable(EditorBrowsableState.Advanced)]
   public class AliasTypeCollection : ArrayList
   {
      public com.test.AliasType Add(com.test.AliasType obj)
      {
         base.Add(obj);
         return obj;
      }

      public com.test.AliasType Add()
      {
         return Add(new com.test.AliasType());
      }

      public void Insert(int index, com.test.AliasType obj)
      {
         base.Insert(index, obj);
      }

      public void Remove(com.test.AliasType obj)
      {
         base.Remove(obj);
      }

      new public com.test.AliasType this[int index]
      {
         get { return (com.test.AliasType) base[index]; }
         set { base[index] = value; }
      }
   }



      [XmlType(TypeName="AliasType",Namespace=Declarations.SchemaVersion),Serializable]
   [EditorBrowsable(EditorBrowsableState.Advanced)]
   public class AliasType
   {

      [XmlElement(ElementName="Type",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string",Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public string __Type;
      
      [XmlIgnore]
      public string Type
      {
         get { return __Type; }
         set { __Type = value; }
      }

      [XmlElement(ElementName="First",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string",Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public string __First;
      
      [XmlIgnore]
      public string First
      {
         get { return __First; }
         set { __First = value; }
      }

      [XmlElement(ElementName="Middle",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string",Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public string __Middle;
      
      [XmlIgnore]
      public string Middle
      {
         get { return __Middle; }
         set { __Middle = value; }
      }

      [XmlElement(ElementName="Last",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string",Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public string __Last;
      
      [XmlIgnore]
      public string Last
      {
         get { return __Last; }
         set { __Last = value; }
      }

      
      public AliasType()
      {
      }
   }


   [XmlType(TypeName="UserType",Namespace=Declarations.SchemaVersion),Serializable]
   [EditorBrowsable(EditorBrowsableState.Advanced)]
   public class UserType
   {

      [XmlElement(ElementName="UserId",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string",Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public string __UserId;
      
      [XmlIgnore]
      public string UserId
      {
         get { return __UserId; }
         set { __UserId = value; }
      }

      [XmlElement(ElementName="Name",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string",Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public string __Name;
      
      [XmlIgnore]
      public string Name
      {
         get { return __Name; }
         set { __Name = value; }
      }

      [XmlElement(ElementName="Email",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string",Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public string __Email;
      
      [XmlIgnore]
      public string Email
      {
         get { return __Email; }
         set { __Email = value; }
      }

      [XmlElement(Type=typeof(com.test.AliasType),ElementName="Alias",IsNullable=false,Form=XmlSchemaForm.Qualified,Namespace=Declarations.SchemaVersion)]
      [EditorBrowsable(EditorBrowsableState.Advanced)]
      public AliasTypeCollection __AliasCollection;
      
      [XmlIgnore]
      public AliasTypeCollection AliasCollection
      {
         get
         {
            if (__AliasCollection == null) __AliasCollection = new AliasTypeCollection();
            return __AliasCollection;
         }
         set {__AliasCollection = value;}
      }

      public UserType()
      {
      }
   }


   [XmlRoot(ElementName="User",Namespace=Declarations.SchemaVersion,IsNullable=false),Serializable]
   public class User : com.test.UserType
   {

      public User() : base()
      {
      }
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 5:17 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
You probably need to implement a custom collection type.
This is a bit involved, what are you striving for?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 10:32 pm 
Newbie

Joined: Tue Feb 20, 2007 12:04 pm
Posts: 16
Thanks for your reply.

Big picture -- I've built all of my business objects via schema/XSD (contract-first), and I'd like to utilize these objects for data storage, business-logic, Web service complex-type responses (XML/JSON), etc. I'm providing a simplistic example here, however, my business model could have 30-50 public types (classes), and I really don't want have to decorate each of these just to use NHibernate for my ORM.

As an aside, I have another XSD --> classes mechanism which will generate strongly-typed arrays rather than ArrayList(s) -- I'm quite sure I could use these with an "array" map and everything would just work. For obvious/multiple reasons, it would be preferable to utilize an ArrayList (Add, Clear, etc.), but it's not looking very doable without some extra work.

Just starting to work with NHibernate, so I appreciate you taking the time to help me here.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 24, 2007 6:24 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
This sound like something that you can do using ICustomCollectionType, but I would recommend just going with arrays, as this would be much simpler.
Using ICustomCollectionType you would need to implement a normal collection and persistable collection, which require a bit of knowledge about NH.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 9:15 am 
Newbie

Joined: Tue Feb 20, 2007 12:04 pm
Posts: 16
Thanks for the advice -- I think I'll stick with strongly-typed arrays as created by my schema-class generator per your suggestion.

Incidentally, I did locate a nice XSD utility that could generate a .Net 2.0 generic List (looking at your work on NHibernate.Generics) instead of a strongly-typed array, as follows:

Code:
        [XmlIgnore()]
        public List<AliasType> AliasCollection
        {
            get
            {
                if ((this.m_aliasCollection == null))
                {
                    this.m_aliasCollection = new List<AliasType>();
                }
                return this.m_aliasCollection;
            }
            set
            {
                this.m_aliasCollection = value;
            }
        }


However, I was never able to get past a cast exception that said something about a GenericList v. PersistentGenericList.


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.