-->
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: Nhibernate error when inserting an objects
PostPosted: Sat Jul 25, 2009 7:20 am 
Newbie

Joined: Fri Sep 28, 2007 3:45 pm
Posts: 9
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

Code:
<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:

Code:
<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:

Code:
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:

Code:
// 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


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.