-->
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.  [ 7 posts ] 
Author Message
 Post subject: Error inserting into one-to-many associations
PostPosted: Thu Sep 09, 2004 6:23 pm 
Newbie

Joined: Wed Sep 08, 2004 5:09 pm
Posts: 3
Hibernate version:2.1

Problem scenario:

We have a Parent object which has a one-to-many relation with the Child object.

In the Parent.hbm.xml file we mentioned that one-to-many relation with the Child object.
Child object has a Parent_ID parameter which maps to Parent's _Id. And in the Child table Parent_ID column is NOT NULL type.

In the Java Bean of Parent.java we have Child as List datamember. i.e something like
Code:
public class Parent
{
           java.long.Long  _Id;
          java.lang.String  _childName;
           ......
           ......
           ......
           List _childList;
   
}

public class Child
{
       java.long.Long _Id;
       java.lang.String  _name;
       java.math.BigDecimal _parentId;
           ......
           ......
           ......
}


Now, I create a Parent object like

parent = new Parent();

parent.setName("parent");

Child child1 = new Child();
child1.setName( "child1");

List childList = new List();
childList.add( child1 );

parent.setChildList( childList );
parent.store(); // THIS GIVES ERROR Parent_Id cannot be NULL



THE ABOVE CODE DOESN'T WORK

Now, when I stoore the Parent object , then what I am expecting is Parent get's a unique Id when it gets inserted into the Parent table, and when it should cacadingly insert the children too with the corresponding Parent_Id.

But, this is not happening, it is throwing me an error saying that NULL cannot inserted into Parent_Id column in the CHILD table .

So, when I tried changing the code a little bit, like
Code:
parent = new Parent();

parent.setName("parent");

parent.store();

Long parentId = parent.getId();

Child child1 = new Child();
child1.setName( "child1");
child.setParentId( parentId );

List childList = new List();
childList.add( child1 );

parent.setChildList( childList );

parent.store(); //THIS STORES IT SUCCESSFULLY INTO CHILD TABLE



I dont understand why the first code snippet doesn't work. Any help is greatly appreciated.
This is impacting our project so, please respond ASAP.


Top
 Profile  
 
 Post subject: Re: Error inserting into one-to-many associations
PostPosted: Fri Sep 10, 2004 11:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Prakash wrote:
This is impacting our project so, please respond ASAP.

This is the best sentence not to have any help.
http://www.hibernate.org/hib_docs/reference/en/html_single/#collections-bidirectional

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 12:14 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
This is impacting our project so, please respond ASAP.

because it is impacting your project we advice you to get some support, here the community don't care about project, just about people, challenge and fun ;)

Note: this is not a rude answer, just an advice

check this http://www.hibernate.org/149.html

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Re: Error inserting into one-to-many associations
PostPosted: Fri Sep 10, 2004 11:52 pm 
Newbie

Joined: Fri Sep 10, 2004 10:53 pm
Posts: 3
Prakash wrote:
Hibernate version:2.1
I dont understand why the first code snippet doesn't work. Any help is greatly appreciated.
This is impacting our project so, please respond ASAP.


You did not post your mapping files. Make sure in the parent.hbm.xml file, while setting the one-to-many relation mapping with child you have cascade property set to "all". After doing this try running the code that was throwing errors.

_________________
- SPS


Top
 Profile  
 
 Post subject: Re: Error inserting into one-to-many associations
PostPosted: Sat Sep 11, 2004 12:23 am 
Newbie

Joined: Wed Sep 08, 2004 5:09 pm
Posts: 3
ssuravarapu wrote:
Prakash wrote:
Hibernate version:2.1
I dont understand why the first code snippet doesn't work. Any help is greatly appreciated.
This is impacting our project so, please respond ASAP.


You did not post your mapping files. Make sure in the parent.hbm.xml file, while setting the one-to-many relation mapping with child you have cascade property set to "all". After doing this try running the code that was throwing errors.


Sorry, I forgot to mention that I have cascade="all" in the association entry. Then also the same problem was arising.


Top
 Profile  
 
 Post subject: Re: Error inserting into one-to-many associations
PostPosted: Sat Sep 11, 2004 7:16 am 
Newbie

Joined: Fri Sep 10, 2004 10:53 pm
Posts: 3
Prakash wrote:
Sorry, I forgot to mention that I have cascade="all" in the association entry. Then also the same problem was arising.


Can you post your mapping files?

_________________
- SPS


Top
 Profile  
 
 Post subject: Re: Error inserting into one-to-many associations
PostPosted: Sun Sep 12, 2004 1:32 pm 
Newbie

Joined: Wed Sep 08, 2004 5:09 pm
Posts: 3
ssuravarapu wrote:
Can you post your mapping files?

Prakash wrote:
EibParent.hbm.xml
-------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.xyz.abc">
<class name="EibParent" table="EIB_PARENT">

<id name="id" column="ID" type="java.lang.Long" unsaved-value="null">
<generator class="seqhilo">
<param name="sequence">EIB_PARENT_SEQ</param>
<param name="max_lo">1</param>

</generator>
</id>

<property name="categoryId" column="CATEGORY_ID" type="java.math.BigDecimal" />
<property name="name" column="NAME" type="java.lang.String" />

<bag name="Child1List" lazy="false" inverse="true" cascade="all">
<key>
<column name="PARENT_ID"/>
</key>
<one-to-many class="EibChild1"/>
</bag>
<bag name="Child2List" lazy="false" inverse="true" cascade="all">
<key>
<column name="PARENT_ID"/>
</key>
<one-to-many class="EibChild2"/>
</bag>
<bag name="Child3List" lazy="false" inverse="true" cascade="all">
<key>
<column name="PARENT_ID"/>
</key>
<one-to-many class="EibChild3"/>
</bag>

</class>

</hibernate-mapping>

----------------------------------------------------------------------------------------

EibChild2.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.xyz.abc">
<class name="EibChild2" table="EIB_CHILD2_V">

<id name="id" column="ID" type="java.lang.Long" unsaved-value="null">
<generator class="seqhilo">
<param name="sequence">EIB_CHILD_SEQ</param>
<param name="max_lo">10</param>

</generator>
</id>

<property name="parentId" column="PARENT_ID" type="java.math.BigDecimal" />
<property name="name" column="NAME" type="java.lang.String" />


</class>

</hibernate-mapping>

------------------------------------------------------------------------------------------------


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.