The following is a simplification of my problem domain. We have a series of Trades, that each gets a record in Valuations every business day.
We filter our Valuations List used for a specific day, but to populate the Trade against each of the Valuation rows, NHibernate fires single row selects on the Trades table for around 50k rows in the valuation table. How can I change this so NHibernate does a single select on the Trade table?
CREATE TABLE Trades
( TradeId INT
, InstrumentType VARCHAR(20)
, TradeDate DATETIME
, PRIMARY KEY
( TradeId ) )
CREATE TABLE Valuations
( TradeId INT
, ValueDate DATETIME
, PresentValue NUMERIC(12,4)
, PRIMARY KEY
( TradeId
, ValueDate ) )
.
class Trade
{
public int TradeId;
public string InstrumentType;
public DateTime TradeDate;
}
class Valuation
{
public int TradeId;
public DateTime ValueDate;
public double PresentValue;
public Trade Trade;
}
.
class ValuationMap : ClassMap<Valuation>
{
public ValuationMap()
{
WithTable("Valuations");
UseCompositeId()
.WithKeyProperty(x => x.ValueDate)
.WithKeyProperty(x => x.TradeId);
Map(x => x.PresentValue);
References(x => x.Trade, "TradeId")
.LazyLoad()
.Cascade.None()
.NotFound.Ignore()
.FetchType.Join();
}
}
class TradeMap : ClassMap<Trade>
{
public TradeMap()
{
WithTable("Trades");
Id( x => x.TradeId );
Map(x => x.InstrumentType);
Map(x => x.TradeDate);
Map(x => x.Valuations);
}
}
.
public List<Valuation> GetValuations(DateTime valueDate)
{
return (from p in _nhibernateSession.Linq<Valuation>()
where p.ValueDate == valueDate
select p).ToList();
}