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: cascade and saving child first?
PostPosted: Fri Jan 30, 2009 11:05 pm 
Newbie

Joined: Sun Jan 25, 2009 4:32 am
Posts: 8
If I have the following mapping files...

<class name="Address">
<id name="Id" column="addressId">
<generator class="assigned" />
</id>
</class>

<class name="Company">
<id name="Id" column="companyId">
<generator class="assigned" />
</id>

<property name="Name" column="[name]" type="String" />

<many-to-one name="Address" class="Address" column="addressId" not-null="true" cascade="all" />
</class>


And I try the following...

Address address = new Address();

Company company = new Company();
company.Name = "Some Company";

company.Address = address;

company.Save();


When I call company.Save() it gives me back a foreign key constraint error because the address hasn't been saved to the database yet. How do I get it to cascade the saving of the address first and then the company?


Any help would be greatly appreciated!

Thanks,
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 6:24 pm 
Newbie

Joined: Sun Jan 25, 2009 4:32 am
Posts: 8
I've found the solution, not sure it's the best choice, but it seems to be working pretty well. I was called .Save( obj ). When I changed it to SaveOrUpdate( obj ) it started producing the correct order of sql inserts/updates.


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.