-->
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: Save parent and associated objects to DB
PostPosted: Tue Jan 25, 2005 9:29 am 
Newbie

Joined: Mon Jan 24, 2005 2:22 pm
Posts: 4
I have a parent class (classA) which has a set of associated objects (classB) in it. classA maps to table A, and classB maps to table B. Table B has Table A's foreign key. In the mapping file, I set the <generator> for the id in both classA and classB as 'identity' (for Microsoft Sql Server).

I've instantiated several classB objects in memory, add them to a set, and add the set to classA object. Now I want to save them to the Database. Here comes the dilemma, how will Hibernate know that it needs to save classA to table A first, so a primary key can be created (the key should be created by database because I set the generator type as 'identity'), and later can be inserted into table B as the foreign key when classB objects are saved? I did set the cascading property. It still didn't work.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 9:35 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Read the documentation, Parent/Child Example chapter.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 2:51 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
Hi Lilly

In class B, if you set the many-to-one mapping to have a cascade of save-update, then you will be able to save a new A just by saving a B which has a reference to it.

If you try that with a cascade of "none" you will get a transient object exception (because A has not been saved yet). In this case, you must save A first before saving B.

HTH

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 12:35 am 
Newbie

Joined: Mon Jan 24, 2005 2:22 pm
Posts: 4
hi ben,

thanks for your reply.. but this still didnt work. ive read the docs as well but can seem to figure it out. here are some more complete details...

table1 mapping
-----------------
<class
name="com.tietronix.hibernateTest.dal.Table1"
table="Table1"
>

<id
name="table1pk"
type="java.lang.Integer"
column="table1pk"
unsaved-value="0"
>
<generator class="identity" />
</id>

<property
name="table1desc"
type="java.lang.String"
column="table1desc"
length="10"
/>

<!-- Associations -->

<!-- bi-directional one-to-many association to Table2 -->
<set
name="table2s"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="table1pk" />
</key>
<one-to-many
class="com.tietronix.hibernateTest.dal.Table2"
/>
</set>

</class>


table2 mapping
-----------------

<class
name="com.tietronix.hibernateTest.dal.Table2"
table="Table2"
>

<id
name="table2pk"
type="java.lang.Integer"
column="table2pk"
unsaved-value="0"
>
<generator class="identity" />
</id>

<property
name="table2desc"
type="java.lang.String"
column="table2desc"
length="10"
/>

<!-- Associations -->

<!-- bi-directional many-to-one association to Table1 -->
<many-to-one
name="table1"
class="com.tietronix.hibernateTest.dal.Table1"
not-null="true"
cascade="save-update"
>
<column name="table1pk" />
</many-to-one>

</class>


code that im running
------------------------

org.hibernate.Session session2 = HibernateUtil.currentSession();

Transaction tx = session2.beginTransaction();



Table2 tbl2_1 = new Table2();
tbl2_1.setTable2desc("GOOGLE");
Set mySet = new HashSet();
mySet.add(tbl2_1);


Table1 tbl1_1 = new Table1();
tbl1_1.setTable1desc("MSN");
tbl1_1.setTable2s(mySet);



session2.save(tbl1_1);
tx.commit();

HibernateUtil.closeSession();

error message that comes up
----------------------------------

22:10:56,471 ERROR [JDBCExceptionReporter] Cannot insert the value NULL into column 'table1pk', table 'hibernateTest.dbo.Table2'; column does not allow nulls. INSERT fails.
22:10:56,502 ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.hibernate.exception.ConstraintViolationException: could not insert: [com.tietronix.hibernateTest.dal.Table2]


Please help.

Lilly


Top
 Profile  
 
 Post subject: obviously!
PostPosted: Wed Jan 26, 2005 2:00 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
hi guy,

i have looked your post though and maybe found out some hints.your two mapping files both contains

<id .....
type="java.lang.Integer"
unsaved-value="0"/>

i think that the default value of objects of Integer should be NULL but not 0.when you create a object from class A or B,the object's id must be NULL,hibernate dosen't make it as unsaved object and the column in db that id maps can't set to be NULL,so error is thrown.you should change your mapping file as following:

<id .....
type="java.lang.Integer"
unsaved-value="NULL"/>


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.