2
votes

a Venue can have one or more Areas and, inversely, each Area has exactly one Venue. I'm using build 1.0.0.636 of Fluent NHibernate.

When I added Fetch.Join() to the VenueMap to eliminate lazy loading, the SQL generated includes a join back to the Area table, from itself: (simplified, obviously)

SELECT *  
FROM Areas  
LEFT OUTER JOIN Venues on Areas.VenueId = Venues.Id  
LEFT OUTER JOIN Areas as Areas2 on Venues.Id = Areas2.VenueId  

This causes it to return duplicate rows so each Venue has duplicate Areas in it's collection. I've tried using MoreLinq's DistinctBy after getting all Areas, but this still leaves each Venue having duplicate Areas - it just removes duplicate areas from the main collection. It also feels really filthy as I'm returning about three times as much data and then throwing it away.

As I alluded to above, my query is to get all Areas and have all Venues eager loaded.

Domain entities:

class Venue
{
int Id;
IList Areas;
}  
class Area
{
int Id;
Venue Venue;
}

Mappings:

public class VenueMap : ClassMap<Venue>  
    {  
        public VenueMap()  
        {  
        Table("Venues");   
                HasMany(v => v.Areas)  
                .KeyColumn("VenueId")  
                .Inverse()  
                .Fetch.Join();  
        }  

public class AreaMap : ClassMap<Area>  
    {  
        public AreaMap()  
        {  
            Table("Areas");  
            Id(a => a.Id);  
            References(a => a.Venue).Column("VenueId").Not.Nullable();  
        }  
    }  

Any ideas how I can sort my mappings out and remove the duplicate Areas? I've done sooo much Googling already...

Thanks, Monty

1

1 Answers

1
votes

So does specifying .Not.LazyLoad() rather than Fetch.Join() in VenueMap on the Areas HasMany not work?

UPDATE What might work better is to get rid of the Fetch.Join() in your mapping and to use a Criteria query like so.

session.CreateCriteria<Area>()
.CreateAlias("Venue", "venue", JoinType.LeftOuterJoin)
.List<Area>()