Hello! I'm trying to understand lazy loading logic in NHibernate and in 
general....
I'm writing an application where i define category and each category can 
have its subcategories.

My Category is class called ComponentClass and subcategory is 
ComponentFeatureA class (all code bellow).

In function GetByName I get ComponentClass by name and it works. FeaturesA 
are not selected, but... what to do if I want to use it? If I properly 
understood it should be automatically loaded when i get a property, so...  
*IList<ComponentFeatureA> 
fal = compClass.FeaturesA as List<ComponentFeatureA>;* should cause select 
to DB? It doesn't work.

What am I doing wrong or what is wrong with my hbms and code...

I really trying to search something in google, but i couldn't. Maybe I just 
missunderstood everything. Please for help smart ones :).

*Function of interest:*
public ComponentClass GetByName(String name)
        {
            ComponentClass compClass = new ComponentClass();

            using (ISession session = NHibernateHelper.OpenSession()) 
            {
                using (ITransaction transaction = 
session.BeginTransaction()) 
                {
                    try
                    {
                        compClass = session.CreateQuery ("from 
ComponentClass cc where cc.Name = :name")
                            .SetParameter ("name", name)
                                .UniqueResult<ComponentClass> ();
                        
                        //NHibernateUtil.Initialize(compClass.FeaturesA);
                        //NHibernateUtil.Initialize(compClass.FeaturesB);

                        compClass.FeaturesA = new List<ComponentFeatureA>();

                        //IList<ComponentFeatureA> fal = 
compClass.FeaturesA as List<ComponentFeatureA>;

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }

            return compClass;
        }

*ComponentClass class:*
public class ComponentClass
    {
        public virtual Int32 ID {get; set;}
        public virtual String Name {get; set;}

        public virtual ICollection<ComponentFeatureA> FeaturesA {get; set;}
        public virtual ICollection<ComponentFeatureB> FeaturesB {get; set;}

        public ComponentClass(Int32 id, String name)
        {
            this.ID = id;
            this.Name = name;
        }

        public ComponentClass(String name)
        {
            this.Name = name;
        }

        public ComponentClass ()
        {
                
        }
    }

*ComponentClass hbm:*
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   assembly="NHibernateUltimateOEE" 
                   namespace="NHibernateUltimateOEE.Domain">
 
      <!-- more mapping info here -->
  
    <class name="ComponentClass">
        <id name="ID">
            <column name="ID" not-null="false"/>
              <generator class="native" />
        </id>
        
        <property name="Name">
            <column name="Name" length="255" not-null="true" unique="true"/>
        </property>
        
        <set name="FeaturesA" cascade="save-update" inverse="true" 
fetch="join">
            <key column="ClassID"/>
            <one-to-many class="ComponentFeatureA"/>
        </set>
        
        <set name="FeaturesB" cascade="save-update" inverse="true">
            <key column="ClassID"/>
            <one-to-many class="ComponentFeatureB"/>
        </set>
      </class>
  
</hibernate-mapping>

*ComponentFeatureA class:*
public class ComponentFeatureA
    {
        public virtual Int32 ID {get; set;}
        public virtual ComponentClass ClassID {get; set;}
        public virtual String Name {get; set;}

        
        public ComponentFeatureA (Int32 id, ComponentClass compClass, 
String name)
        {
            this.ID = id;
            this.ClassID = compClass;
            this.Name = name;
        }

        public ComponentFeatureA (ComponentClass compClass, String name)
        {
            this.ClassID = compClass;
            this.Name = name;
        }

        public ComponentFeatureA ()
        {
        }
    }

*ComponentFeatureA hbm:*
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   assembly="NHibernateUltimateOEE" 
                   namespace="NHibernateUltimateOEE.Domain">
 
    <!-- more mapping info here -->
  
    <class name="ComponentFeatureA">
        <id name="ID">
            <column name="ID" not-null="false"/>
              <generator class="native" />
        </id>
        <many-to-one name="ClassID" column="ClassID" 
unique-key="UNIQ_FEATA"/>
        <property name="Name">
            <column name="Name" length="255" not-null="true" 
unique-key="UNIQ_FEATA"/>
        </property>
    </class>
</hibernate-mapping>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/KhYH-BUqdUoJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to