(I have asked a similar question in the German forum, so bear with me...)
Hi All,
I have a flat table structure that needs to be filled from a hierarchical object layout. This object structure not only contains dependent objects, of which I know that I can map them using components, but also a collection of dependent objects which have no unique key on their own. I am looking to create a mapping which duplicates field values of the parent objects (and its components) for each of the objects in the collection. All examples that I found so far describe the mapping as such that the collection is being persisted into a separate table, however, this is no option for me, as the target database cannot be modified.
Let me give an example:
My class hierarchy:
Code:
public class StationRecord
{
#region Private Members
private long id;
private string station;
private IList<Measurement> measurements;
#endregion
#region Properties
/// <summary>
/// My primary key ?
/// </summary>
public virtual long ID
{
get { return id; }
set { id = value; }
}
public virtual string Station
{
get { return station; }
set { station = value; }
}
public virtual IList<Measurement> Measurements
{
get { return measurements; }
set { measurements = value; }
}
#endregion
#region Constructor(s) / finaliser
public StationRecord()
{
id = -1;
station = String.Empty;
measurements = new List<Measurement>();
}
#endregion
} // end class StationRecord
// ================================= //
public class Measurement
{
#region Private Members
private DateTime timestamp;
private double concentration;
private double temperature;
private double pressure;
#endregion
#region Properties
public virtual DateTime Timestamp
{
get { return timestamp; }
set { timestamp = value; }
}
public virtual double Concentration
{
get { return concentration; }
set { concentration = value; }
}
public virtual double Temperature
{
get { return temperature; }
set { temperature = value; }
}
public virtual double Pressure
{
get { return pressure; }
set { pressure = value; }
}
#endregion
#region Constructor(s) / finaliser
public Measurement(double concentration, double temperature, double pressure)
{
timestamp = DateTime.Now;
this.concentration = concentration;
this.temperature = temperature;
this.pressure = pressure;
}
#endregion
} // end class Measurement
The table structure:
- bigint ID (PK)
- varchar(50) Station
- datetime Timestamp
- decimal Concentration
- decimal Temperature
- decimal Pressure
and the table entries for multiple measurements at the same station I would expect like this:
- 1,StationA,2010-08-30 14:33:22.123,0.7,58.2,1003
- 2,StationA,2010-08-30 14:33:28.321,0.71,58.2,1004
- 3,StationB,2010-08-30 14:34:01.411,0.801,65.0,978
- ...
Is it possible to map this using NHibernate or do I have to create helper objects to achieve this?
Many thanks in advance for any suggestions.
Jörg