Hi all,
I have a problem with nhibernate when I try to insert an object with an one-to-one relationship to another object. There is a class called Article
Mapping file
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<id name="ID" column="article_id" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Title" column="title" type="String" not-null="true" length="255" />
<property name="Body" column="body" type="String" length="1073741823" />
<property name="IsEnabled" column="is_enabled" type="Boolean" not-null="true" />
<many-to-one name="ParentArticle" class="..." column="parent_id" cascade="all" />
<bag name="Children" lazy="true" cascade="all-delete-orphan" >
<key column="parent_id" />
<one-to-many class="..." />
</bag>
The class itself follows from this mapping file and is omitted for the sake of simplicity.
Then there is another class called SpecialArticle. The mapping for this class is as follows:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<id name="ID" column="id" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Price" column="Price" type="Double" not-null="true" />
<one-to-one name="Article" class="..." fetch="join" cascade="all" />
The ArticleExtension class then is:
public class SpecialArticle
{
/// <summary>
/// Get/Set the ID for this question
/// </summary>
public virtual int ID
{
get;
set;
}
/// <summary>
/// Get/Set the expected price for this question or answer
/// </summary>
public virtual Double Price
{
get;
set;
}
/// <summary>
/// Get/Set the article that contains the rest of the information for this question
/// </summary>
public virtual Article Article
{
get;
set;
}
}
when i want to insert a special article in the db i do:
// Populate someArticle
// Populate someSpecialArticle
// Following code is wrapped in a transaction
someSpecialArticle.Article = someArticle;
Session.SaveOrUpdate(someArticle);
Session.SaveOrUpdate(someSpecialArticle);
Object someArticle is inserted ok, but the insert statement for object someSpecialArticle is: INSERT INTO tbl (Price) VALUES (@p0); select SCOPE_IDENTITY()
where is the article_id to specify the parent article???
thx