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.  [ 4 posts ] 
Author Message
 Post subject: How to mapping with List<T> type?
PostPosted: Sat Sep 08, 2007 11:10 pm 
Newbie

Joined: Sat Sep 08, 2007 10:23 pm
Posts: 1
I have code like this:
Code:
    [Serializable()]
    public class Role : GenericDomain<Guid>
    {
        private string _Name;

        public virtual string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _Description;

        public virtual string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        private List<FunctionModuleBase> _Modules;

        public virtual List<FunctionModuleBase> Modules
        {
            get { return _Modules; }
            set { _Modules= value; }
        }
    }


Mapping like this:
Code:
  <class name="SMSSystem.Domain.Role" table="T_Role">
    <id name="ID" type="Guid">
      <column name="U_UID" sql-type="uniqueidentifier" not-null="true" unique="true"/>
      <generator class="guid" />
    </id>
    <property name="Name" type="string">
      <column name="S_Name" sql-type="varchar" length="200"/>
    </property>
    <property name="Description" type="string">
      <column name="S_Description" sql-type="varchar" length="2000"/>
    </property>
    <bag name='FunctionModules' table='T_RoleFunction' cascade='all' lazy='true' inverse='true' generic='true'>
      <key column='U_RoleUID'/>
      <many-to-many class="SMSSystem.Domain.FunctionModuleBase" not-found="ignore" lazy="proxy">
        <column name="U_FunctionUID" sql-type="uniqueidentifier"/>
      </many-to-many>
    </bag>
  </class>


But, it throw a exception when get the entity:
NHibernate.MappingException: Invalid mapping information specified for type SMSSystem.Domain.Role, check your mapping file for property type mismatches

System.InvalidCastException: Can not cast type “NHibernate.Collection.Generic.PersistentGenericBag`1[SMSSystem.Domain.FunctionModuleBase]” to type“System.Collections.Generic.List`1[SMSSystem.Domain.FunctionModuleBase]”

If I change the entity use IList<T>
Code:
        private IList<FunctionModuleBase> _Modules;

        public virtual IList<FunctionModuleBase> Modules
        {
            get { return _Modules; }
            set { _Modules = value; }
        }


It works fine.
How can I mapping property use List<T> type?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 09, 2007 12:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
You cannot map to List<T>, it must be IList<T>. One way around that is to implement your own NHibernate.UserType.IUserCollectionType for the mapping.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 10, 2007 10:14 am 
Beginner
Beginner

Joined: Thu Oct 26, 2006 4:45 am
Posts: 39
You can define your private property in your class with a generic list:

Code:
private IList<FunctionModuleBase> _Modules = new List<FunctionModuleBase>();

        public virtual IList<FunctionModuleBase> Modules
        {
            get { return _Modules; }
            set { _Modules= value; }
        }


If you want to have generic List from NHibernate for certain criteria, you can make a general method for it like this:
Code:
public static List<T> GetObjectList<T>(ICriteria criteria)
        {
            try
            {
                // Make results distinct
                criteria.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer());

                List<T> objectList = new List<T>(criteria.List<T>());

                //And sort on default comparer
                if (objectList is IComparable)
                {
                    objectList.Sort();
                }

                return objectList;
               
            }
            catch (HibernateException ex)
            {
                // Log the error to database for future
                // reference...
                return new List<T>(); // act as if we did not find anything
            }
   }


Last edited by hace_x on Mon Sep 10, 2007 10:57 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 10, 2007 10:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The reason one has to use an IList (which is an interface) instead of a List (which is a concrete class) is because NHibernate replaces the collection at run-time to facilitate lazy loading. The NHibernate-generated collection implements the IList interface but does not extend the List class. That explains the InvalidCastException.

_________________
Karl Chu


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