These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Collection Mapping - The Ugly Way
PostPosted: Fri Sep 03, 2010 3:37 am 
Newbie

Joined: Thu Sep 02, 2010 10:43 pm
Posts: 2
Location: Cyprus
(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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.