-->
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.  [ 2 posts ] 
Author Message
 Post subject: SOLVED: Can't save child by saving parent (again!)
PostPosted: Fri Jan 08, 2010 1:51 pm 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
I had a very similar problem yesterday and fixed it (with help) but after refactoring something very similar seems to have come back to haunt me!

I have a parent/child relentionship and would like to save the parent and have the child automatically persisted/saved.

Parent
Code:
<set name="habitTrackerTrackerUserDatas" inverse="true" lazy="true" table="habitTrackerTrackerUserData" cascade="persist,merge,save-update" fetch="select">
            <key>
                <column name="tracker_id" />
            </key>
            <one-to-many class="uk.co.prodia.prosoc.domainmodel.HabitTrackerTrackerUserData" />
        </set>


Child
Code:
<many-to-one name="habitTrackerTracker" class="uk.co.prodia.prosoc.domainmodel.HabitTrackerTracker" fetch="select">
            <column name="tracker_id" />
        </many-to-one>


Hibernate is performing the following SQL statements:

Code:
insert into habittracker.habitTrackerTracker (yaxis_start, yaxis_end, yaxis_increment) values (?, ?, ?);
update habittracker.habitTrackerTrackerUserData set user_id=?, tracker_id=?, tracker_name=? where user_data_id=?


If I save the parent the following happens:

  1. It saves the parent;
  2. Then tries to update the child which fails as the child has not yet been saved;
  3. A RollbackException is thrown and the data is not committed to the DB.

If I save the child this happens instead:

  1. It tries to save the child but fails as the parents id is null.

Any suggestion or links to docs appreciated. I have looked through several parent/child documents from the manual as well as the Transitive persistence but I am obviously missing something.


Last edited by klogger on Sat Jan 09, 2010 12:02 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Can't save child by saving parent (again!)
PostPosted: Sat Jan 09, 2010 12:01 pm 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
The error wasn't in the hbm.xml files. The problem was that somewhere between Flex/Spring-BlazeDS/Dozer and Hibernate the Hibernate domain Object was having its id set to 0. When Hibernate tried to save it, it failed but didn't give an error message. The id should have been left un-initialised.

My solution was simply to only set the id in the Dozer DTO if the id passed in was > 0.


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