简体   繁体   中英

nHibernate returns double the number of records

My query is returning exactly double amount of rows I expect. When I look at nHibernate profiler, I see the query I expect, but it appears twice in the profiler...

Mappings:

A Location has many LocationRooms which has many LocationRoomLayouts:

Location

 public LocationMap()
 {
    Id(x => x.locationID).Column("ID");

    HasMany(x => x.LocationRooms)
       .KeyColumn("LID") 
       .Not.LazyLoad().Cascade.All();
 }

Location Room

 public LocationRoomMap ()
 {
      References(x => x.Location).Column("LID");
      HasMany(x => x.LocationRoomLayouts)
          .KeyColumn("RID")
          .Not.LazyLoad().Cascade.All();
 }

Location Room layout

 public LocationRoomLayoutMap()
 {
       References(x => x.LocationRoom).Column("RID");
 }

the query that is returning double the rows:

 var locations = session.CreateCriteria<LocationRoomLayout>("p")
                        .Add(Restrictions.Eq("CostPerHour", 0))
                        .Add(Restrictions.Eq("CostPerDay", 0))
                        .Add(Restrictions.Eq("CostPerHalfDay", 0))
                        .Add(Restrictions.Eq("CostPerPerson", 0))
                         .List<LocationRoomLayout>();

NHibernate uses the resultset that it has retrieved from the database to build the entities. Since your entities consist of data that is coming from different tables, the database returns multiple times the same record from your 'master' table.

Therefore, multiple entities are returned. Use the DistinctRootEntityTransformer to make sure that NHibernate only returns distinct entities.

More information can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM