Hello there,
I have two objects associated.
First one, Category:
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace byow.Objects
{
public class Category
{
private int _categoryId = int.MinValue;
private int _number = int.MinValue;
private IList<CategoryLocale> _arrLocale = new List<CategoryLocale>();
public virtual int CategoryId
{
get { return _categoryId; }
set { _categoryId = value; }
}
public virtual int Number
{
get { return _number; }
set { _number = value; }
}
public virtual IList<CategoryLocale> Locales
{
get { return _arrLocale; }
set { _arrLocale = value; }
}
}
}
And then CategoryLocale:
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace byow.Objects
{
public class CategoryLocale
{
private int _categoryLocaleId = int.MinValue;
private string _name = string.Empty;
private Category _category = null;
private LocLanguage _locLanguage = null;
public virtual int CategoryLocaleId
{
get { return _categoryLocaleId; }
set { _categoryLocaleId = value; }
}
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
public virtual Category Category
{
get { return _category; }
set { _category = value; }
}
public virtual LocLanguage LocLanguage
{
get { return _locLanguage; }
set { _locLanguage = value; }
}
}
}
Now the mapping files:
Category:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="byow.Objects" assembly="byow.Objects">
<class name="Category" table="Categories">
<id name="CategoryId" type="Int32" column="categoryId">
<generator class="identity"/>
</id>
<property name="Number" column="number" type="System.Int32" />
<bag name="Locales" table="CategoriesLocale">
<key column="categoryId" />
<one-to-many class="CategoryLocale" />
</bag>
</class>
</hibernate-mapping>
CategoryLocale:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="byow.Objects" assembly="byow.Objects">
<class name="CategoryLocale" table="CategoriesLocale">
<id name="CategoryLocaleId" type="Int32" column="categoryLocaleId">
<generator class="identity"/>
</id>
<property name="Name" column="name" type="String" />
<many-to-one name="Category" column="categoryId" />
<many-to-one name="LocLanguage" column="locLanguageId" />
</class>
</hibernate-mapping>
If I do:
Code:
// create Locale
CategoryLocale cl = new CategoryLocale();
cl.Name = "brown";
// create and assign language to Locale
cl.LocLanguage = new LocLanguage();
cl.LocLanguage.LocLanguageId = 1;
cl.LocLanguage.CultureName = "en-CA";
// create Category
Category cat = new Category();
cat.Number = 222;
// add Locale to Category
cat.Locales.Add(cl);
cl.Category = cat;
// save Category
this.NHSession.SaveOrUpdate(cat);
It persists only the Category object in the database. The CategoryLocale object (which was created and added to the IList collection) is not saved in its table.
I need to explicitly call:
this.NHSession.SaveOrUpdate(cl);
Is that correct? Is there a better way to save everything (Category and its children CategoryLocale) with 1 step?
Thanks