0

I have a series of classes that are mapped to their respective database objects.

In the mapping, the fetch mode is set so that associated lookups are loaded with the parent record.

I'm now referencing some of these objects, and as such I'm obviously getting the affiliated records eagerly loaded.

If possible I don't want to use lazy loading, as I'm passing these objects from a service layer - not ideal, but I'm hoping I can deal with this at a query level.

Take this example:

var c = s.CreateCriteria(typeof(Transaction))
                        .Add(Restrictions.Eq("Id", myId))
                        .SetFetchMode("Remark", FetchMode.Join)
                        .SetFetchMode("Category", FetchMode.Join)
                        .SetFetchMode("Reason", FetchMode.Join);

var t = c.List<Transaction>().FirstOrDefault();

Remark has many records associated with it, these are currently being loaded when I run this query.

Can you modify the ICriteria to prevent the reference entities loading their associations?

In this example, I only want Transction and its directly associated Remark, Category and Reason

Thanks

sambomartin
  • 6,663
  • 7
  • 40
  • 64
  • According to this http://stackoverflow.com/q/3199032/1236044 using aliases, I guess you may try to override the FetchMode of the children's properties, from Eager to Lazy. It may be a lot of properties to override anyway... – jbl Aug 27 '13 at 12:52
  • I think it's generally better to leave `lazy` and `fetch` alone in the mappings, and instead deal with them at the query level - just like you're trying to do. I don't think you'll be able to do this as long as the mappings are set up this way. – Daniel Schilling Aug 28 '13 at 03:43

1 Answers1

0

on the top of my head it should be

var c = s.CreateCriteria(typeof(Transaction))
                    .Add(Restrictions.Eq("Id", myId))
                    .CreateAlias("Remark", "remark"))
                    .SetFetchMode("remark.Collection", FetchMode.Lazy);
Firo
  • 30,626
  • 4
  • 55
  • 94
  • Using this technique, the referenced object is still Fetched when it is accessed. Is there a `FetchMode` or equiv that will only fetch when i ask it? – sambomartin Aug 29 '13 at 10:32
  • I'm not sure i understand. Is fetching Remark or Collection the problem? Accessing an object is asking it to initialize itself. Otherwise it would be broken. – Firo Aug 30 '13 at 08:10