2011-07-27 8 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_ など

Shouldnですそれは少なくとも...より一貫している?

それは編集---マッピング

内「」や「式」のいずれかの種類を使用するのはとても難しい(不可能)になり:私はマッピング

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の」と呼ばれる性質を持っているあなたは、このようにマッピングでどこ制約を作成することができます。

Where("SomeValue = 1"); 

同様のことは、XMLファイルのマッピングのために適用されます。

+0

問題は、私は数式を持っている結合されたテーブルを持っていると、この結合されたテーブルにバインドする必要があります。 (table3.id = table2.t3idのtable3からmaxを選択してください) 'とtable1にはt3idがありますので、別名を使用しないと..列があいまいです。数式で 'where'を除いてすべてのレコードを取ります – buddy

+0

これはどこのマッピングとコードを使用できますか? – eulerfx

+0

が投稿に追加されました。本当に最新のものではありませんでした。 – buddy

関連する問題