簡體   English   中英

流利的Nhibernate多對多關系問題

[英]Fluent Nhibernate Many to Many relationship issue

我的情況是我的數據庫中有三個表。 一個包含客戶,一個包含客戶組,然后一個表將它們鏈接在一起。

我遇到了我的nHibernate查詢無法正確加入它們的問題,並且不確定是否丟失了某些東西。

映射客戶群

public CustomerGroupMapping() {
      Table("tbCustomerGroups");
      // TODO: Revisit Lazy loading
      //LazyLoad();
      Id(x => x.customerGroupId)
        .GeneratedBy.Identity()
        .Column("Customer_Group_ID");
      Map(x => x.customerGroup)
        .Column("Customer_Group")
        .Length(50);
      Map(x => x.editDisabled)
        .Column("EditDisabled")
        .Not.Nullable();

      HasManyToMany<CustomerModel>(x => x.customers)
        .Table("tbCustomerGroupsRelationship")
        .ParentKeyColumn("Customer_Group_ID")
        .ChildKeyColumn("Customer_ID")
        .Inverse();
    }

為客戶映射

public CustomerMapping() {
  Table("tbCustomers");
  // TODO: Revisit Lazy load
  LazyLoad();
  Id(x => x.customerId)
    .GeneratedBy.Identity()
    .Column("Customer_ID");
  Map(x => x.customerTypeId)
    .Column("Customer_Type_ID")
    .Not.Nullable()
    .Precision(10);

  // A Customer has many users
  HasMany<UserModel>(x => x.users)
    .KeyColumn("Customer_ID");

  // A Customer can belong to many groups
  HasManyToMany<CustomerGroupModel>(x => x.groups)
    .Table("tbCustomerGroupsRelationship")
    .ParentKeyColumn("Customer_ID")
    .ChildKeyColumn("Customer_Group_ID")
    .Not.Inverse();
}

問題似乎是當我獲得客戶時,我想看到該客戶的組客戶。 (即IE給我客戶10,得到那個擁有客戶10的客戶組,然后給我該組中的所有其他客戶)

有沒有一種方法可以更改這些映射以使其正確加載而不生成大量的select語句,因為這就是Log4net向我展示的內容。

您應該使用的是批處理獲取

19.1.5。 使用批量提取

簡而言之,此優化設置會將大量查詢減少為幾批。 它提高了性能,並支持對一個根實體的查詢(無需將FETCH策略用作SELECT)。

擴展您的類,並使用.BatchSize(xx)集合映射。 類應具有以下映射:

public CustomerGroupMapping() 
{
    Table("tbCustomerGroups");
    BatchSize(25);
    ...

public CuustomerMapping() 
{
    Table("tbCustomers");
    BatchSize(25);
    ...

館藏應擴大

HasManyToMany<CustomerModel>(x => x.customers)
   ...
   .BatchSize(25)

HasManyToMany<CustomerGroupModel>(x => x.groups)
   ...
   .BatchSize(25)

還要檢查以下類似內容:

另外,我的建議是-不要使用多對多。 我希望配對表是一級對象...也許檢查一下:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM