具有限制的NHibernate CreateCriteria无法按预期工作

时间:2012-11-15 03:25:20

标签: nhibernate fluent-nhibernate hibernate-onetomany createcriteria

下面给出的代码示例是检索所有活动记录。

session.CreateCriteria<VesselMasterData>()
                        .CreateAlias("BasicInfo", "bsInfo")
                        .CreateAlias("DimentionInfo", "diInfo")
                        .Add(Restrictions.Eq("IsActive", 1))
                        .Add(Restrictions.Eq("diInfo.IsActive", 1))
                        .Add(Restrictions.Eq("MasterDataID", masterDataID))
                        .Add(Restrictions.Eq("bsInfo.IsActive", 1))
                        .List<VesselMasterData>()

父表VesselMasterData和子表是BasicInfo和DimentionInfo。 根据查询,预期值应该是BasicInfo和DimentionInfo的所有活动记录。

但是在输出中我得到了BasicInfo和DimentionInfo的所有记录。可能是什么问题。

这是VesselMasterData表的映射。

public class VesselMasterDataMap : ClassMap<VesselMasterData>
    {
        public VesselMasterDataMap()
        {
Table("VPD_VESSEL_MASTER_DATA");
            Schema("APPLN1");
            Id(x => x.MasterDataID).Column("MASTER_DATA_ID").GeneratedBy.Sequence("VPD_VESSEL_MASTER_DATA_SEQ");
            Map(x => x.VesselCode).Column("VESSEL_CODE");
            Map(x => x.IsActive).Column("IS_ACTIVE");
HasMany<VesselBasicInfo>(prop => prop.BasicInfo)
                    .KeyColumns.Add("MASTER_DATA_ID").Cascade.SaveUpdate().Inverse().Not.LazyLoad();
            HasMany<VesselDimension>(prop => prop.DimentionInfo)
                    .KeyColumns.Add("MASTER_DATA_ID").Cascade.SaveUpdate().Inverse().Not.LazyLoad();
        }
   }

2 个答案:

答案 0 :(得分:0)

使用Restrictions.And将加入条件添加为:

       session.CreateCriteria<VesselMasterData>()
              .CreateAlias("BasicInfo", "bsInfo")
              .CreateAlias("DimentionInfo", "diInfo")
              .Add(Restrictions.And(
                     Restrictions.Eq("IsActive", 1),
                     Restrictions.And(
                         Restrictions.Eq("diInfo.IsActive", 1),
                         Restrictions.And(
                              Restrictions.Eq("MasterDataID", masterDataID),
                              Restrictions.Eq("bsInfo.IsActive", 1)
                         )
                     )
                  )
               ).List<VesselMasterData>()

答案 1 :(得分:0)

这是预期的方式,但期待获得等效的CreateCriteria代码。

    session.CreateQuery(string.Format("from VesselMasterData vm inner join fetch 
vm.AcquisitionInfo acq inner join fetch vm.BasicInfo bi inner join fetch 
vm.DimentionInfo di inner join fetch vm.SaleInfo si 
where vm.MasterDataID={0}  and bi.IsActive = 1 and di.IsActive = 1 ", masterDataID)).Future<VesselMasterData>();