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.  [ 5 posts ] 
Author Message
 Post subject: Help with simple one-to-many insert
PostPosted: Sat Dec 10, 2005 12:40 am 
Newbie

Joined: Sat Dec 10, 2005 12:23 am
Posts: 5
I have a parent class (Item) and child a class (Attribute) with one-to-many relationship. When I try to save Item that has a number of child Attributes, I get the following error:

Quote:
Cannot insert the value NULL into column 'item_id', table 'ST_Attribute'; column does not allow nulls. INSERT fails. The statement has been terminated.


I know that if I save Item first and then save its Attributes it will work, as we'll have a value for Item's Id. But I can't believe there isn't a way in NHibernate to handle it. I'm pretty sure I'm missing something in my mapping files, but can't figure out what. Any help would be greatly appreciated.

Following are the mapping files:

1. Item

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="DomainObjects" assembly="DomainObjects">
    <class  name="Item" table="ST_Item">
        <id name="Id" column="id" unsaved-value="0">
            <generator class="identity"/>
        </id>
        <property name="Name" column="name"/>
        <bag name="Attributes" cascade="all-delete-orphan" inverse="true">
            <key column="item_id"/>
            <one-to-many class="Attribute"/>
        </bag>
    </class>
</hibernate-mapping>


2. Attribute

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="DomainObjects" assembly="DomainObjects">
    <class  name="Attribute" table="ST_Attribute">
        <id name="Id" column="id" unsaved-value="0">
            <generator class="identity"/>
        </id>
        <property name="Name" column="name"/>
        <property name="Value" column="value"/>
        <many-to-one name="Item" class="Item" column="item_id" cascade="none"/>
        <many-to-one name="AttributeType" class="AttributeType" column="attribute_type_id"/>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 10, 2005 7:41 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
I can't find any major fault in the mappings (I would add a lazy attribute to the bag and a not-null attribute to the many-to-one). I suspect that is something wrong with the way you create and add the attribute objects to the item object. when you add an attribute to the Attributes collection you have to set its Item property with the parent item object.
Please post the code.

Cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 10, 2005 11:06 am 
Newbie

Joined: Sat Dec 10, 2005 12:23 am
Posts: 5
Thanks! You were absolutely right. I wasn't setting Item to Attribute's Item property. Works now.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 2:57 pm 
Beginner
Beginner

Joined: Mon Oct 02, 2006 6:46 pm
Posts: 32
I think I am having the same problem but I don't understand what needs to be done by radu's response.

I have an orders object that has a one-to-many relationship with my orderlines object. When I populate orders and its orderlines and call .Save on order (with cascade on) the orderid is generated for orders, but isn't propagated to orderlines and I get the "Cannot insert the value NULL into column" error.

Here are my mapping files:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="HELM.Domains.Orders,HELM.Domains" table="orders" lazy="false">
    <id name="Orderid" column="Orderid" type="System.Int32">
      <generator class="native"/>
    </id>
    --<<...snipped out irrelevant fields...>>--
    <!-- Relationships-->
    <bag name="Orderlines" lazy="false" inverse="true" cascade="all">
      <key column="Orderid" />
      <one-to-many class="HELM.Domains.Orderlines, HELM.Domains" />
    </bag>   
  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="HELM.Domains.Orderlines,HELM.Domains" table="orderlines" lazy="false">
    <id name="Orderid" column="OrderID" type="System.Int32">
      <generator class="assigned"/>
    </id>
    --<<...snipped out irrelevant fields...>>--
    <property column="OrderLineID" name="OrderLineid" type="System.Int32" not-null="true"/>
  </class>
</hibernate-mapping>


appreciate any help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 7:32 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
In your mappings, the Primary Key (Id) of Orderline should be OrderLineID and not Orderid. OrderId is a foreignkey and usually you map it in your orderline object using many-to-one and not as a int property.

After you fix the maps, you have to check in your code that when you create and insert a new orderline to your order, you also assign to your new orderline object the parent order object:
Code:

Orderline orderline=new Orderline();
order.Orderlines.Add(orderline);
orderline.Order=order;


Also I would suggest to make the Orderline collection lazy.

hth,
Radu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

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.