First some remarks... Do not perform *ANY* session or transaction management in your entities!
1. Your code is performing a filter on a child collection. Either, do this in your parent entity 2. You fetch an entity from the database and then replace the FeaturesA with a new collection which is not what you should do. Just do .Clear() on the existing collection. 3. You MUST NOT cast an ICollection to an IList. Either the type is a set (<set>) or a list (<bag>). 4. Make your default constructors protected as in most cases these are only needed for NHibernate. Your mapping seems rather ok although it is pretty hard to read. What is the exception that you are getting? I would think that the as List<ComponentFeatureA> would fail as you try to convert an set to a list which will result in NULL. Don't do a as List<XXX> but perform an actual cast as when that fails you know that there is a problem with the source type. -- Ramon On Fri, Oct 5, 2012 at 2:27 PM, Brajan <[email protected]> wrote: > 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. > -- You received this message because you are subscribed to the Google Groups "nhusers" group. 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.
