2011-07-27 34 views
2

有沒有什麼辦法可以預測當nhibernate生成sql時將爲表生成什麼別名?NHibernate - sql表別名不一致

理論上它應該是'this_','this_1_'等等,它會很棒,但我發現當我在映射中使用Join時,它會更隨機一些。

例如,在集成測試我得到這樣的:

table1 this_ 
left outer join 
    table2 o this_1_ 
     on this_.id=this_1_.t1id 
left outer join 
    table3 this_2_ 
     on this_.id=this_2_.t1id 
left outer join 
    table4 this_3_ 
     on this_.id = this_3_.t1id 
left outer join 
    table5 somealiasbasedonrootentity_2_ 
     on this_.id=somealiasbasedonrootentity_2_.t1id 

但是當我運行在Web服務器相同的映射......所有的別名是somealias_,somealias_1_ 等

不該它至少......更一致嗎?

它使真的很難(甚至不可能)使用任何類型的「其中」或「公式」的映射中

---編輯:示例查詢我想實現與映射

select 
    t1.a, t2.a, (select max(t3.value) where t3.id=t2.t3id) 
from table1 t1 
    left join table2.t2 on t2.t1id=t1.id; 

---編輯這裏(幾乎最新)的映射: 我知道這是相當大的,但我打算將它用於報告目的。 (順便說一句這不是很最新版本)

public class ClaimHistoryMap : ClassMap<ClaimHistory> 
    { 
     public ClaimHistoryMap() 
     { 
      Table("wts_claim"); 
      ReadOnly(); 
      Not.LazyLoad(); 
      Id(x => x.Id, "claimid"); 
      Map(x => x.ClaimNo, "claimNo"); 
      Map(x => x.DateCompleted, "ModificationDate"); 
      Map(x => x.DateOfDispatch, "DateOfDispatch"); 
      Map(x => x.DateProcessed, "ModificationDate"); 
      Map(x => x.Status, "status"); 
      Map(x => x.WorkOrderNo, "ServiceWorkOrder"); 
      Map(x => x.SerialNo, "serialNo"); 
      Map(x => x.IsEnabled, "bStatus"); 
      Map(x => x.InvoiceNo, "InvoiceNo"); 
      Map(x => x.ServiceCoverage).Formula(
       @"(Select c.coveragename from wts_servicecoverage as c where c.servicecoverageid=servicecoverageid)"); 
      Join("wts_site_info",x=> 
          { 

            x.Optional().KeyColumn("claimid"); 
            x.Map(s => s.CustomerName, "CustomerName"); 
            x.Map(s => s.CustomerAddress, "Address"); 
            x.Map(s => s.CustomerCity, "City"); 
            x.Map(s => s.CustomerPhone, "Phone"); 
            x.Map(s => s.CustomerZip, "Zip"); 
            x.Map(s => s.ReportComplaint, "Complaint"); 
            x.Map(s => s.TechnicianName, "TechName"); 

            x.Map(s => s.Model) 
             .Formula(@"(Select mo.Model from WTS_Product mo where mo.ProductId=this_1_.ProductId)"); 
            x.Map(s => s.CustomerState) 
             .Formula(@"(Select st.statename from wts_state st where st.stateid=this_1_.state)"); 
           }); 
     Join("wts_grand_total", x => 
            { 
             x.Optional().KeyColumn("claimid"); 
             x.Map(s => s.TotalCharge, "total"); 
             x.Map(s => s.FreightCharge, "Frieght"); 
             x.Map(s => s.PartsCharge, "Parts"); 
             x.Map(s => s.HandlingFee, "Handling"); 
             x.Map(s => s.SalesTax, "Mix"); 
            }); 
     Join("wts_labour_travel", x => 
     { 
      x.Optional().KeyColumn("claimid"); 
      x.Map(s => s.TravelCharge).Formula("traveltotal+travelovertotal+MilageRegular+MilageOvertime+supmileagehour+supmileageoverhour"); 
      x.Map(s => s.TravelTime).Formula("TravelHourRegular+TravelHourOvertime+suptravelhour+suptraveloverhour"); 
      x.Map(s => s.LaborCharge).Formula("labortotal+laborovertotal"); 
      x.Map(s => s.LaborTime).Formula("LaborHoursRegular+LaborHoursOvertime+suplaborhour+suplaboroverhour"); 
      x.Map(s => s.TripsNo, "trips"); 
      x.Map(s => s.TruckCharge).Formula(
       "(select max(ltr.TruckRate) from wts_labour_travel_rate ltr where ltr.LabourTravelId = this_3_.LabourTravelId)"); 
     }); 

     Map(x => x.WasModified).Formula(
      "(select count(comm.claim_id) from wts_claim_status comm where comm.claim_id=this_.claimid and comm.Status=3)"); 
     References(x => x.User, "entryBy").Fetch.Join().Not.LazyLoad(); 

     HasMany(x => x.PartNo).KeyColumn("claimid").Table("wts_general_part").Element("partNo"); 
     HasMany(x => x.Repairs).KeyColumn("claimid").Table("wts_Claim_Resolution").Element("resolutionDesc"); 
    } 
} 

回答

2

你不應該需要引用別名映射,只是引用您感興趣的屬性。例如,如果你有一個類FluentNHibernate映射來有一個名爲「someValue中」屬性,你可以創建一個在mapping限制這樣的:

Where("SomeValue = 1"); 

類似的事情將適用於XML文件映射。

+0

問題是我已經加入了表,它有一個公式,它必須綁定到這個連接表。所以它的東西就像'(從table3選擇max(value)where table3.id = table2.t3id)',而table1也有t3id,所以如果我不使用任何別名。列是不明確的。並且沒有公式中的「哪裏」,它將採取所有記錄 – buddy

+0

您可以提供映射和代碼在何處使用? – eulerfx

+0

添加在帖子中,不是最新的(我正在擺弄)所以它是從scm – buddy