1

In the entity framework we can achieve eagar loading by using "include". I have written the following query for eagar loading in NHibernate linq.

IList empl = Session.CreateCriteria(typeof(Employee))
                      .Add(Expression.Like("Name", "Pete%"))
                      .SetFetchMode("Name", FetchMode.Eager)
                      .SetFetchMode("Desigantion", FetchMode.Eager)
                      .List();

Can any one give me better example of eagar and lazy loading in the NHibernate Linq. I have following Mapping in fluent Nhibernate:

Table("DEMO_Employee");
            Id(t => t.Id).Column("Id").GeneratedBy.Identity();
            Map(t => t.Name, "Name");
            Map(t => t.Designation, "Designation");
            Map(t => t.Gender, "Gender");
            Map(t => t.Age, "Age");
            Map(t => t.Enabled, "Enabled");
            Map(t => t.CreatedById).Column("CreatedBy");
            Map(t => t.LastModifiedById).Column("LastModifiedBy").Nullable();
            Map(t => t.IsDeleted).Column("IsDeleted");
            Map(t => t.CreatedDate).Column("CreatedDate");
            Map(t => t.LastModifiedDate).Column("LastModifiedDate").Nullable();
            References(x => x.Department).ForeignKey("DeptId"); 



Table("DEMO_Department");
            Id(t => t.DeptId).Column("DeptId").GeneratedBy.Identity();
            Map(t => t.DeptName, "DeptName");
            Map(t => t.Enabled, "IsEnable");
Bhupendra Shukla
  • 3,814
  • 6
  • 39
  • 62

1 Answers1

1

Take a look at the 19.1.2. Tuning fetch strategies, a cite:

Usually, we don't use the mapping document to customize fetching. Instead, we keep the default behavior, and override it for a particular transaction, using left join fetch in HQL. This tells NHibernate to fetch the association eagerly in the first select, using an outer join. In the ICriteria query API, you would use SetFetchMode(FetchMode.Join).

The mapping and the query above do not fit together. We do not have to use Fetch on the value type properties. Just on the relations. So, this .SetFetchMode("Name".. would not be needed

So, in your mapping the

References(x => x.Department)

is correct. We do not specify any default fetch strategy. We leave it on the default (lazy) setting.

Once we start to querying, we can change the lazy loading (similar to include) by using these features:

session.CreateCriteria(typeof(Employee))
    // will be part of the SELECT clause... not ready for querying
    .SetFetchMode("Department", FetchMode.Join)
    // if needed for querying ... 
    .CreateCriteria("Department", "Dep", JoinType.LeftOuterJoin)
    // or join alias, which is similar but we are working with the original criteria
    .CreateAlias("Department", "Dep")

All these approaches will result in one SELECT, joining all the results.
see NHibernate - CreateCriteria vs CreateAlias

If we would query just the Employee, all the related stuff would be loaded lazily

I would say, that unless we need the related stuff for querying (then use CreateCriteria) we should use the Lazy loading as possible. But to avoid the 1 + N scenarios, we can use the batching. This is in a detail described here: 19.1.5. Using batch fetching

To use the batching with fluent mapping, you can mark your class like this:

Table("DEMO_Employee");
Id(...
...
BatchSize(25);

And then your related data will be also loaded lazily, but in batches. The same goes for one-to-many/HasMany (collections)

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Once again thanks for the answer. Actually i want to implement the eagar loading. Can you provide my the way to achieve this by providing me any code or reference. – Bhupendra Shukla Jan 09 '14 at 06:29
  • 1
    Please, take a look and read this blog post: http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate. Ayende is the NHibernate "Guru", so these posts do have THE ANSWERS. I am sure that you touched the 15.5 - dynamic fetching (eager loading) http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate. So, use the `SetFetchMode()` or `CreateAlias()`... to eagerly load stuff. But please, try to observe the `BatchSize` in the link I gave you. I do use that instead of eager loading, for ages... I am more then happy and thankful. – Radim Köhler Jan 09 '14 at 06:40