views:

116

answers:

0

Hi,

I have a two classes that I'm trying to map:

public class Data
{
    public virtual int Id {get;set;}
    public virtual ValueObject Primary {get;set;}
    public virtual IList<ValueObject> Secondary {get;set;}

    public virtual void SetPrimary(ValueObject valueObject)
    {
        valueObject.Data = this;
        Primary = data;
    }

    public virtual void AddSecondary(ValueObject valueObject)
    {
        valueObject.Data = this;
        Secondary.Add(data);
    }

}

public class ValueObject
{
    public virtual int Id {get;set;}
    public virtual Data Data {get;set;}
    public virtual double Value {get;set;}
    public virtual DateTime Date {get;set;}
}

The mapping I've come up with so far doesn't work as the primary doesn't get saved:

The Data class:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="Data, Sample" table="Data">
     <id name="Id" column="Id" type="System.Int32" unsaved-value="0">
      <generator class="assigned"/>
     </id>

     <one-to-one name="Primary" class="ValueObject, Sample"
 cascade="all-delete-orphan" 
foreign-key="none"/>

     <bag name="Secondary" 
       cascade="all-delete-orphan" 
       lazy="true" 
       inverse="true">
      <key column="Data_Id" foreign-key="data_values_fk"/>
      <one-to-many class="Data, Sample"/>
     </bag>  
    </class>
</hibernate-mapping>

The ValueObject class:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="ValueObject, Sample" table="ValueObjects">
     <id name="Id" column="Id" type="System.Int32" unsaved-value="0">
      <generator class="native"/>
     </id>

     <many-to-one name="Data" 
         class="Data,Sample" 
         column="Data_Id"
         foreign-key="values_Data_fk"/>

     <property name="Date" type="Date" access="property" column="Date"/>

     <property name="Value" type="Double" access="property" column="Value"/>
    </class>
</hibernate-mapping>

I then save data like this:

Data d = new Data();
d.SetPrimary(new ValueObject{ Date=DateTime.UtcNow,  Value=3});
d.AddSecondary(new ValueObject{ Date=DateTime.UtcNow,  Value=5});
DataRepository.Save(d);

When I get the data from the database again it has no primary value and two secondary values.

How can I map this correctly??

Thanks, nisbus

related questions