2

I have the following model

public class BaseEntity
{
    public virtual int EntityID { get;set; }
    public virtual bool Active { get;set; }
}

public class Brand : BaseEntity
{
   public Brand()
   {
      Series = new List<Series>();
   }

   public virtual string BrandName { get;set; }
   public virtual string Website { get;set; }
   public virtual IList<Series> Series;
}

public class Series: BaseEntity
{
   public Series()
   {
      Products = new List<Product>();
   }

   public virtual Brand Brand { get;set; }
   public virtual string SeriesName { get;set; }
   public virtual string SeriesDescription { get;set; }
   public virtual IList<Product> Products;
}

public class Product : BaseEntity
{
   public Product()
   {
      Series = new List<Series>();
   }

   public virtual Series Series { get;set; };
   public virtual string ProductName { get;set; }
   public virtual string ProductCode { get;set; }
   public virtual string ProductDescription { get;set; }
   public virtual double SellingPrice { get;set; }
}

I have set all mappings to not lazy load.

public class BrandMapping() : ClassMap<Brand>
{

    public BrandMapping()
    {
       Table("Brand");
       Id(item => item.EntityID).Column("BrandID").GeneratedBy.Increment();
       Map(item => item.BrandName).Not.LazyLoad();
       Map(item => item.Active).Not.LazyLoad();
       Map(item => item.Website).Not.LazyLoad();
       HasMany<Series>(item => item.Series).Cascade.All().Not.LazyLoad();
    }
}

public class SeriesMapping() : ClassMap<Series>
{

    public SeriesMapping()
    {
       Table("Series");
       Id(item => item.EntityID).Column("SeriesID").GeneratedBy.Increment();
       Map(item => item.SeriesName).Not.LazyLoad();
       Map(item => item.SeriesDescription).Not.LazyLoad();
       Map(item => item.Active).Not.LazyLoad();
       HasMany<Product>(item => item.Products).Cascade.All().Not.LazyLoad();
       Reference<Brand>(item => item.Brand).Column("BrandID");
    }
}


public class ProductMapping() : ClassMap<Product>
{

    public ProductMapping()
    {
       Table("Product");
       Id(item => item.EntityID).Column("ProductID").GeneratedBy.Increment();
       Map(item => item.ProductName).Not.LazyLoad();
       Map(item => item.ProductDescription).Not.LazyLoad();
       Map(item => item.ProductCode).Not.LazyLoad();
       Map(item => item.SellingPrice).Not.LazyLoad();
       Map(item => item.Active).Not.LazyLoad();
       Reference<Series>(item => item.Series).Column("ProductID");
    }
}

I have a generic Repository base that has the following code for the Load method so I don't need to create code for each typed Repo:

IEnumerable<T> Load()
{
    IList<T> results = new List<T>();

    StartTransaction();
    results = _session.CreateCriteria(typeof(T)).List<T>();
    CommitTransaction();

    return results;
}

My issue is that one of my screens displays products. This screen needs to display the Series and Brand names for completeness in the grid. I have found that even with lazy loading disabled, the Series is not even loaded, never mind the Brand record. I need to find a way to add the SetFetchMode to the above load code to ensure that all relationship trees (Product->Series->Brand) are loaded when a record is loaded from the DB.

Anyone got ideas how I can do generic SetFetchmode?

1
  • What about your mapping classes? Commented May 30, 2013 at 6:43

1 Answer 1

1

It would be easiest to specify eager loading in the mapping files for each entity, this is done by adding the Fetch specification.

If you are using Fluent nHibernate then this should do it:

  References(x => x.Series).Column("SeriesId").ForeignKey("Id").Fetch.Join();

If you put this inside your Product mapping it tells nHiberate to eager load the Series whenever a Product is loaded.

Likewise put something similar in your Series to load the Brand.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.