New to FNH and NH.
I'd like to optimize a query for a OneToMany mapping. I'd actually like to only retrieve one of the many where Segment = 0. PlantID,AreaID,CellID,DeviceID,StartDateTime make up the composite primary key for Data. Add Segment to composite key for DataMore1 and DataMore2.
An event can record up to 400 data points...depending on the options they have chosen. So we've elected to store the data in separate tables for purging of unwanted data after x days or months.
The goal is to do something like this via FluentNHibernate.
SELECT Data.PlantID, Data.AreaID, Data.CellID, Data.DeviceID, Data.StartDateTime, Data.DataPoint01, DataMore1.Segment, DataMore1.DataPoint101,
DataMore2.DataPoint201
FROM Data
LEFT OUTER JOIN
DataMore1 ON Data.PlantID = DataMore1.PlantID AND Data.AreaID = DataMore1.AreaID AND Data.CellID = DataMore1.CellID AND
Data.DeviceID = DataMore1.DeviceID AND Data.StartDateTime = DataMore1.StartDateTime AND **DataMore1.Segment = 0**
LEFT OUTER JOIN
DataMore2 ON Data.PlantID = DataMore2.PlantID AND Data.AreaID = DataMore2.AreaID AND Data.CellID = DataMore2.CellID AND
Data.DeviceID = DataMore2.DeviceID AND Data.StartDateTime = DataMore2.StartDateTime AND **DataMore2.Segment = 0**
The MODEL
public class Data
{
public virtual int PlantID { get; set; }
public virtual int AreaID { get; set; }
public virtual int CellID { get; set; }
public virtual int DeviceID { get; set; }
public virtual DateTime StartDateTime { get; set; }
public virtual float DataPoint01 { get; set; }
public virtual float DataPoint02 { get; set; }
public virtual float DataPoint99 { get; set; }
public virtual IEnumerable<DataMore1> DataMore1 { get; set; }
public virtual IEnumerable<DataMore2> DataMore2 { get; set; }
}
public class DataMore1
{
public virtual int PlantID { get; set; }
public virtual int AreaID { get; set; }
public virtual int CellID { get; set; }
public virtual int DeviceID { get; set; }
public virtual DateTime StartDateTime { get; set; }
public virtual byte Segment { get; set; }
public virtual float DataPoint101 { get; set; }
public virtual float DataPoint102 { get; set; }
public virtual float DataPoint199 { get; set;}
}
public class DataMore2
{
public virtual int PlantID { get; set; }
public virtual int AreaID { get; set; }
public virtual int CellID { get; set; }
public virtual int DeviceID { get; set; }
public virtual DateTime StartDateTime { get; set; }
public virtual byte Segment { get; set; }
public virtual float DataPoint201 { get; set; }
public virtual float DataPoint202 { get; set; }
public virtual float DataPoint299 { get; set; }
}
And the MAPPINGS
public DataMap()
{
Table("Data");
CompositeId()
.KeyProperty(e => e.PlantID)
.KeyProperty(e => e.AreaID)
.KeyProperty(e => e.CellID)
.KeyProperty(e => e.DeviceID)
.KeyProperty(e => e.StartDateTime);
Map(e => e.DataPoint01);
Map(e => e.DataPoint02);
Map(e => e.DataPoint99);
HasMany(e => e.DataMore1)
.KeyColumns.Add("PlantID")
.KeyColumns.Add("AreaID")
.KeyColumns.Add("CellID")
.KeyColumns.Add("DeviceID")
.KeyColumns.Add("StartDateTime")
.Cascade.All().Inverse();
HasMany(e => e.DataMore2)
.KeyColumns.Add("PlantID")
.KeyColumns.Add("AreaID")
.KeyColumns.Add("CellID")
.KeyColumns.Add("DeviceID")
.KeyColumns.Add("StartDateTime")
.Cascade.All().Inverse();
}
public DataMore1Map()
{
Table("DataMore1");
CompositeId()
.KeyProperty(e => e.PlantID)
.KeyProperty(e => e.AreaID)
.KeyProperty(e => e.CellID)
.KeyProperty(e => e.DeviceID)
.KeyProperty(e => e.StartDateTime)
.KeyProperty(e => e.Segment);
Map(e => e.DataPoint101);
Map(e => e.DataPoint102);
Map(e => e.DataPoint199);
}
public DataMore2Map()
{
Table("DataMore2");
CompositeId()
.KeyProperty(e => e.PlantID)
.KeyProperty(e => e.AreaID)
.KeyProperty(e => e.CellID)
.KeyProperty(e => e.DeviceID)
.KeyProperty(e => e.StartDateTime)
.KeyProperty(e => e.Segment);
Map(e => e.DataPoint201);
Map(e => e.DataPoint202);
Map(e => e.DataPoint299);
}