3

I have two classes:

public class Reference
{
  public virtual string Id { get; set; }
  // ... 
  public virtual Stamp Stamp { get; set; }
}

public class Stamp
{
  public DateTime? Created { get; set; }
  public User CreatedBy { get; set; }
  public DateTime? LastUpdated { get; set; }
  public User LastUpdatedBy { get; set; }
}

Reference.Stamp is mapped as a component (so that the "Stamp" fields appear in the Reference table) using the following fluent nhibernate mappings:

public class ReferenceMap : ClassMap<Reference>
{
  public ReferenceMap()
  {
    Id(e => e.Id);
    // ...
    Component(e => e.Stamp);
  }
}

public class StampMap : ComponentMap<Stamp>
{
  public StampMap()
  {
    Map(e => e.Created);
    References(e => e.CreatedBy);
    Map(e => e.LastUpdated);
    References(e => e.LastUpdatedBy);
  }
}

I want to do an eager fetch on the "CreatedBy" and "LastUpdatedBy" fields, to avoid an N+1 when listing my "Reference" items.

I tried this:

Reference[] references = session
  .Query<Reference>()
  .Fetch(r => r.Stamp.CreatedBy)
  .Fetch(r => r.Stamp.LastUpdatedBy)
  .ToArray();

Which causes this error:

System.ArgumentException: A fetch request must be a simple member access expression of the kind o => o.Related; 'r.Stamp.CreatedBy' is too complex.

Any ideas as to how I could get my fetch working?

Jonathan Moffatt
  • 13,309
  • 8
  • 51
  • 49

1 Answers1

4

I believe this is a limitation of the LINQ provider.

You can either:

  • Not use Fetch (there are better alternatives to solve the N+1 problem; I prefer batching)
  • Map those properties directly, without a Component
  • Use HQL instead of LINQ

Still, I believe your construct should be supported. You can open an issue at https://nhibernate.jira.com

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • 6
    It will be executed if you write it like `.Fetch(r => r.Stamp).ThenFetch(r => r.CreatedBy)`. But although this doesn't throw an exception, it also doesn't work (doesn't affect the generated SQL). See this issue: https://nhibernate.jira.com/browse/NH-2769 – cremor Sep 26 '12 at 12:56