Nhibernate加入另一张桌子

时间:2012-11-23 04:14:47

标签: nhibernate c#-4.0 fluent-nhibernate

我需要对客户进行加入。其中address是primary并填充PrimaryAddress1和PrimaryCity字段。客户映射已经与地址类有很多关系,但我不想获取所有地址(性能问题)。

请帮助..

班级:

public class Customer
{
    public Customer()
    {
        Addressess = new List<Address>();
    }
   public virtual int CustomerID { get; set; }
    public virtual int? BranchID { get; set; }
    public virtual int? CustTypeID { get; set; }
    public virtual string CompanyName { get; set; }
    public virtual string Prefix { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string MiddleName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string PrimaryAddress1 { get; set; }
    public virtual string PrimaryCity { get; set; }
    public virtual List<Address> Addresses { get; set; }
}


public class Address
{
    public Address()
    {
    }
    public virtual int LocationID { get; set; }
    public virtual int? CustomerID { get; set; }
    public virtual string LocationName { get; set; }
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string Address3 { get; set; }
    public virtual string City { get; set; }
    public virtual bool Primary { get; set; }
}

映射:

public TblCustomerMap()
{
    Table("tblCustomers");
    LazyLoad();
    Id(x => x.CustomerID).GeneratedBy.Identity().Column("CustomerID");
    Map(x => x.ProfileID).Column("ProfileID");
    Map(x => x.BranchID).Column("BranchID");
    Map(x => x.DateEntered).Column("DateEntered");
    Map(x => x.DateTerminated).Column("DateTerminated");
    Map(x => x.CustTypeID).Column("CustTypeID");
    Map(x => x.CompanyName).Column("CompanyName").Not.Nullable().Length(50);
    Map(x => x.Prefix).Column("Prefix").Not.Nullable().Length(50);
    Map(x => x.FirstName).Column("FirstName").Not.Nullable().Length(50);
    Map(x => x.MiddleName).Column("MiddleName").Not.Nullable().Length(50);
    Map(x => x.LastName).Column("LastName").Not.Nullable().Length(50);
    HasMany(x => x.Address).KeyColumn("CustomerID");
    Map(x => x.PrimaryAddress1).Column("PrimaryAddress1") // from table tbladdress where address is primary and get data from address1 column
    Map(x => x.PrimaryCity).Column("PrimaryCity") // from table tbladdress where address is primary and get data from city column
}

查询:

 var query = session
            .QueryOver<Customer>(() => customer)
            .JoinQueryOver(() => customer.Addresses, () => address)
            .Where(() => address.Primary)
            .List();

        foreach (var customer1 in query)
        {
            customer1.PrimaryAddress1 = customer1.Addresses[0].Address1;
            customer1.PrimaryCity = customer1.Addresses[0].City;
            customer1.PrimaryState = customer1.Addresses[0].StateOrProvince;

        }

新查询:

  var query = session.Query<Customer>()
            .SelectMany(c => c.Addresses,
                        (c, a) => new {c, a})
            .Where(cust => cust.a.Primary)
            .Select(item => new CustomerView()
                              {
                                 CustomerID = item.c.CustomerID,
                                 CompanyName=  item.c.CompanyName,
                                 FirstName=  item.c.FirstName,
                                  LastName=item.c.LastName,
                                  Address1=item.a.Address1,
                                  Address2=item.a.Address2,
                                 Address3= item.a.Address3,
                                 City= item.a.City,
                                 StateOrProvince= item.a.StateOrProvince
                              });
        return query.ToList();

2 个答案:

答案 0 :(得分:3)

有(至少)两种方法来实现这一目标。

1)扩展数据库和调整 NHibernate 映射更直观,更易读。我们需要创建新的视图 viewPrimaryAddress

SELECT ***columns*** FROM [tbladdress] WHERE Primary = true

客户映射将如下所示:

Join("[viewPrimaryAddress]", 
{
  m.Fetch.Join();
  m.KeyColumn("CustomerID");
  m.Map(t => t.PrimaryAddress1).Column("PrimaryAddress1");
  m.Map(t => t.PrimaryCity).Column("PrimaryCity");
});

就是这样。将发布一个SQL语句,因此无需加载地址集合

2)第二种方法的概述

第二种方法将通过新的类映射创建该视图,有点复杂,但只能在应用程序端(C#和NHiberante)完成。

将创建新类PrimaryAddress并在类级别定义中包含ilter(xml映射示例:

<class name="PrimaryAddress" ... where="Primary = true" >

然后我们可以将Customer扩展为{em>多对一与PrimaryAddress的关系。因此,获取列“PrimaryAddress1”和“PrimaryCity”的属性将通过WHERE子句中的SQL select过滤完成。

EXTENDED:

后续步骤应指导您如何创建新映射,将主要地址定位为一对一属性 1)C#PrimaryAddress:

public class PrimaryAddress
{
    public virtual Customer Customer { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
}

2)映射:

public TblPrimaryAddressMap()
{
    Table("tbladdress");
    LazyLoad();
    Where("Primary = 1");
    // Id as AddressId
    References(x => x.Customer).Column("CustomerID");
    Map(x => x.Address).Column("PrimaryAddress1")
    Map(x => x.PrimaryCity).Column("PrimaryCity")
}

3)Customer

的新属性
public class Customer
{  
   ..
   public virtual PrimaryAddress PrimaryAddress { get; set; }

4)Customer的新映射 public TblCustomerMap() {   ...   HasOne(x =&gt; x.PrimaryAddress)

此时,当您从会话中获得Customer时。你可以访问

customer.PrimaryAddress.Address
customer.PrimaryAddress.City

我主要使用XML映射。但是从这些方面来看,这个概念应该是清楚的......玩一些fetch="join"你可以在一个SQL选择中加载Customer及其PrimaryAddress

如果您甚至需要客户属性Customer.PrimaryAddress1,只需在获取者中包装PrimaryAddress.Address。

您可以获得更多,可以通过此新属性PrimaryAddress

进行过滤和排序

注意:这种方法对于缓存来说很脆弱。原因是,你将改变真正的Address实体,没有建立机制来驱逐PrimaryAddress。此外,您应该强制业务层使仅允许一个地址设置为primary = true

的客户

答案 1 :(得分:2)

我有一个类似的地址结构并且在努力解决这个问题。一个主要问题是如果客户没有主要地址(不应该发生......),则确保返回客户记录。

如果要在Customer实体中包含地址字段,请查看join mapping

我的方法是使用数据传输对象(DTO或视图),在本例中为CompanyDto:

    internal static QueryOver<Company> GetCompanyDtoQuery()
    {
        Address addr = null;
        CompanyDto dto = null;

        var query = QueryOver.Of<Company>()
            .Left.JoinAlias(c => c.Addresses, () => addr)
            .SelectList(list =>
                        {
                            list.Select(c => c.CompanyId).WithAlias(() => dto.CompanyId)
                            list.Select(a -> addr.AddressId).WithAlias(() => dto.AddressId
                            return list;
                        })
            .Where(a => addr.AddressId == null || addr.IsPrimaryAddress)
            .TransformUsing(new AliasToDtoDeepTransformer<CompanyDto>("PrimaryAddress"));
        return query;
    }

[编辑]

使用Query更容易做到这一点,例如:

            var target = (from c in session.Query<Company>()
                          from a in c.Addresses
                          where a.IsPrimaryAddress
                          select new
                              {
                                  c.CompanyId,
                                  a.Address1
                              })
                .Take(10).ToList();
            Assert.IsTrue(target.Any());

您发布的查询的问题是您通过索引访问集合来强制延迟加载。您可以使用预先加载来获取一个选择中的所有地址:

             var target = session.QueryOver<Company>()
                                 .Fetch(c => c.Addresses).Eager
                                 .Take(10).List();
             Assert.IsTrue(target.Any());

但是,正如我在原始回复中所说,您应该查看加入或其他方法,而不是在域模型中包含其他表格中的字段。