1
votes

I have an Entity which holds a collection of OtherEntity in a many-to-many style relationship. The mapping for this property looks like:

HasManyToMany(x => x.OtherEntity)
                .AsBag()
                .Table("EntityToOtherEntityMapping")
                .ParentKeyColumn("EntityId")
                .ChildKeyColumn("OtherEntityId")
                .Not.LazyLoad()
                .Cascade.None();

I notice that when retrieving a collection of Entity's there's a seperate SQL query for each collection of OtherEntity.

How can I have Fluent-NHibernate execute this retrieval in one query rather than n?

2

2 Answers

2
votes

Add fetch join on your property.

.Fetch.Join();
0
votes

The answer turned out to be in setting the FetchModel to Eager and selecting a ResultTransformer:

.SetFetchMode("Tags", FetchMode.Eager)
.SetResultTransformer(Transformers.DistinctRootEntity)