2011-12-05 55 views
0

我有一個列表Funds,其中每個都有一個ID,ConfigIDCountryID。根據這些ID,我需要爲每個Fund選擇FundPerformances如何使用Fluent NHibernate和QueryOver查詢複合主鍵?

FundPerformance表具有ID的組合主鍵,ConfigID & CountryID

到目前爲止,我有

perfs = _session.QueryOver<FundPerformance>() 
      .Where(xx => xx.ConfigId == _configId) 
      .Where(Restrictions.In(Projections.Property<FundPerformance>(t => t.Id), funds.Select(xx => xx.Id).ToArray())) 
      .Where(Restrictions.In(Projections.Property<FundPerformance>(t => t.CountryId), funds.Select(xx => xx.CountryId).ToArray())) 
      .List<FundPerformance>().ToList(); 

我不認爲這是去工作,因爲它的選擇對個人領域,而不是複合ID。

我需要怎麼做才能正確選擇數據?

這是當前FundPerformance映射類:

public class FundPerformanceMap : ClassMap<FundPerformance> 
{ 
    public FundPerformanceMap() 
    { 
     Id(x => x.Id).Column("FundPerformanceStatistics_FundId"); 
     Map(x => x.CountryId).Column("FundPerformanceStatistics_FundCountryId"); 
     Map(x => x.ConfigId).Column("FundPerformanceStatistics_ConfigId"); 

     Map(x => x.OneMonth).Column("FundPerformanceStatistics_OneMonthBack"); 
     Map(x => x.ThreeMonth).Column("FundPerformanceStatistics_ThreeMonthBack"); 
     Map(x => x.YTD).Column("FundPerformanceStatistics_YearToDate"); 
    } 
} 

回答

1

也許你正在尋找其他的東西

public class Fund 
{ 
    public virtual int Id { get; set; } 
    public virtual Country Country { get; set; } 

    public virtual Config Config { get; set; } 

    public virtual IDictionary<Config, FundPerformance> Performances { get; private set; } 
    public virtual FundPerformance ActualPerformance { get { return Performances[Config]; } } 

} 

public class FundPerformance 
{ 
    public virtual Fund Fund { get; set; } 
    public virtual Config Config { get; set; } 

    public virtual int OneMonth { get; set; } 
    public virtual int ThreeMonth { get; set; } 
    public virtual int YTD { get; set; } 
} 

public class Config 
{ 
    public virtual int Id { get; set; } 
} 

public class FundMap : ClassMap<Fund> 
{ 
    public FundMap() 
    { 
     CompositeId() 
      .KeyProperty(x => x.Id, "FundId") 
      .KeyProperty(x => x.CountryId, "FundCountryId"); 

     HasMany(x => x.Performances) 
      .KeyColumns.Add("FundPerformanceStatistics_FundId", "FundPerformanceStatistics_FundCountryId") 
      .AsMap(fp => fp.Config) 
      .Cascade.AllDeleteOrphan() 
      .Component(c => 
      { 
       c.ParentReference(x => x.Fund); 
       c.References(x => x.Config, "FundPerformanceStatistics_ConfigId"); 
       c.Map(x => x.OneMonth, "FundPerformanceStatistics_OneMonthBack"); 
       c.Map(x => x.ThreeMonth, "FundPerformanceStatistics_ThreeMonthBack"); 
       c.Map(x => x.YTD, "FundPerformanceStatistics_YearToDate"); 
      }) 
    } 
} 

// Query 
var someCountry = session.Load<Country>(5); <- get the Country with id 5 without going to the db (proxy when not already loaded) only to have the reference 

var key = new Fund { Id = 1, Country = someCountry } 
var fund = session.Get<Fund>(key); 
var performanceByConfig = fund.Performances[_config]; 

原來的答覆:

如果基金有compositeKey然後FundPerformance應該有一個也是。此外,我認爲一個參考基金更OOP樣:

public class FundPerformance 
{ 
    public virtual Fund Fund { get; set; } 

    public virtual int OneMonth { get; set; } 
    public virtual int ThreeMonth { get; set; } 
    public virtual int YTD { get; set; } 
} 

無論它映射爲組件(因爲它看起來像是依賴)

public class FundMap : ClassMap<Fund> 
{ 
    public FundMap() 
    { 
     CompositeId() 
      .KeyProperty(x => x.Id, "FundId") 
      .KeyProperty(x => x.CountryId, "FundCountryId") 
      .KeyProperty(x => x.ConfigId, "ConfigId"); 

     HasMany(x => x.Performances) 
      .KeyColumns.Add("FundPerformanceStatistics_FundId", "FundPerformanceStatistics_FundCountryId", "FundPerformanceStatistics_ConfigId") 
      .Cascade.AllDeleteOrphan() 
      .Component(c => 
      { 
       c.ParentReference(x => x.Fund); 
       c.Map(x => x.OneMonth, "FundPerformanceStatistics_OneMonthBack"); 
       c.Map(x => x.ThreeMonth, "FundPerformanceStatistics_ThreeMonthBack"); 
       c.Map(x => x.YTD, "FundPerformanceStatistics_YearToDate"); 
      }) 
    } 
} 

或作爲一個實體與複合鍵

public class FundPerformanceMap : ClassMap<FundPerformance> 
{ 
    public FundPerformanceMap() 
    { 
     CompositeId() 
      .KeyReference(x => x.Fund, param => param 
       .Column("FundPerformanceStatistics_FundId") 
       .Column("FundPerformanceStatistics_FundCountryId") 
       .Column("FundPerformanceStatistics_ConfigId")); 

     Map(x => x.OneMonth).Column("FundPerformanceStatistics_OneMonthBack"); 
     Map(x => x.ThreeMonth).Column("FundPerformanceStatistics_ThreeMonthBack"); 
     Map(x => x.YTD).Column("FundPerformanceStatistics_YearToDate"); 
    } 
} 

// Query 
var key = new Fund { Id = 1, CountryId = 2, ConfigId = 3} 
var fund = session.Get<Fund>(key); 
var performances = fund.Performances; 
+0

Hi @Firo,謝謝你。對不起,我現在纔回來,但今天大部分時間我都被其他東西纏住了。儘管如此,我有點困惑。你已經說過'Fund'應該有'CompositeId'映射來包含'ConfigId'。我不確定這會起作用,因爲基金錶沒有ConfigId列。它只在FundPerformance中。這會起作用嗎?或者我可以只指定一個ID和CountryID的Fund CompositeId映射嗎? – DaveDev

+0

你的第一句話:「我有一個基金清單,每個基金都有一個ID,ConfigID和一個CountryID。」讓我想你有。那麼在Fundperformance中configId的目的是什麼? – Firo

+0

對不起 - 我不打算讓它混淆。 FundPerformance中的ConfigID與計算性能的方式有關。基金有一個財產配置ID,用於確定它應該選擇哪種績效。 – DaveDev