I have class PersonClass that embeds other classes Name and Address (composition). I save the name and address in separate tables from the parent, I am able to save the parent class and it saves the name and address (if any) but when I try to retrieve the parent, the name and address are null.
I believe I am totally misusing the Component and the One-to-One elements. I will appreciate any help or pointers to examples/documentation to read.
My schema looks like below:
table contact
(
[id] int not null identity,
[name_id] int,
[address_id] int
)
table name
(
[id] int not null identity,
[fname] varchar(25),
[lname] varchar(25)
)
table address
(
[id] int not null identity,
[street] varchar(25),
[zip] varchar(25)
)
<class name="Tests.PersonClass" table="contact">
<id name="id" type="Int32" >
<generator class="identity" />
</id>
<component name="person_name" class="Tests.NameClass ">
<property name="id" column="name_id" not-null="false" />
</component>
<component name="person_address" class="Tests.AddressClass ">
<property name="id" column="address_id" not-null="false"/>
</component>
<one-to-one name="person_name" class="Tests.NameClass" cascade="all" constrained="false" lazy="false"/>
<one-to-one name="person_address" class="Tests.Address" cascade="all" constrained="false" lazy="false"/>
</class>
<class name="Tests.NameClass" table="name">
<id name="id" column="name_id" type="Int32" >
<generator class="identity" />
</id>
<property name="fname" column="first_name"/>
<property name="lname" column="last_name"/>
</class>
Code:
public class PersonClass
{
private Nullable<int> _id;
private NameClass _myName;
private AddressClass _myAddress;
public PersonClass() { }
public virtual Nullable<int> id
{
get { return this._id; }
set { this._id = value; }
}
public virtual NameClass person_name
{
get { return this._myName; }
set { this._myName = value; }
}
public virtual AddressClass person_address
{
get { return this._myAddress; }
set { this._myAddress = value; }
}
public virtual Nullable<int> name_id
{
get { return this._myName == null ? null : this._myName.id; }
set { if (this._myName != null)this._myName.id = value; }
}
public virtual Nullable<int> address_id
{
get { return this._myAddress == null ? null : this._myAddress.id ; }
set { if (this._myAddress!=null)this._myAddress.id = value; }
}
}
public class NameClass
{
private Nullable<int> _id;
private string _fname;
private string _lname;
public NameClass(){}
public virtual Nullable<int> id
{
get { return this._id; }
set { this._id = value; }
}
public virtual string fname
{
get { return this._fname; }
set { this._fname = value; }
}
public virtual string lname
{
get { return this._lname; }
set { this._lname = value; }
}
}