-->
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.  [ 8 posts ] 
Author Message
 Post subject: Update a tranisent object that containts a collection wo PKs
PostPosted: Fri Feb 25, 2005 12:03 pm 
Newbie

Joined: Fri Feb 25, 2005 10:51 am
Posts: 8
Hi,
I am trying to figure out how to update a chain of objects without knowing their PK.
Lets say I have two tables (and objects) County and State. State contains counties (parent-child assoc). Both tables use a surrogate key for PK but they both have unique constraints. In the application I want to update the tables using data from a different datasource (same Java objects but without id). If the tables are empty I can use session.save(state) and the state and it's containing counties are saved (with cascade attribute). But how should I update the table records if they already exists? I have tried to find the stored instance first by using a bussiness key and then set the new state's id to this id and update. This works fine but the counties are not updated new rows are inserted (which violates the county tables unique constraint). I understand why the new rows are inserted (since all county objects ids are null) but I don't know how to solve it nice? I would like to avoid using composite-ids and I would also like to avoid iterating through the county collection because in reality the county objects referes to other transient objects and these to others etc...


Thanks,
William


Top
 Profile  
 
 Post subject: Re: Update a tranisent object that containts a collection wo
PostPosted: Fri Feb 25, 2005 12:13 pm 
Newbie

Joined: Fri Apr 16, 2004 9:27 am
Posts: 18
Location: Russia, Spb
First, needs to perform search by value (county name in your case). And if result set is empty then save your object by call save(). Thus, there are no any composite id and needless object loading.


Top
 Profile  
 
 Post subject: Re: Update a tranisent object that containts a collection wo
PostPosted: Fri Feb 25, 2005 1:54 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
d99-vsp wrote:
I would like to avoid using composite-ids

It seems to me that composite-ids would be just the thing. Why do you wish to avoid them? I think it is better to solve this in your mapping (getting proper unique ids for objects) than adding complexity to wherever you need this in your code.
It is quite possible, though, that the previous respondent's solution is faster.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 3:51 pm 
Newbie

Joined: Fri Feb 25, 2005 10:51 am
Posts: 8
Hi,
Thank you both for your replies,
Composite ids actually works pretty good for State where I can use name or abbreviation. For county I don't know if the names are unique if not I have to use both name and state. Anyways I simplified the model a bit in the post. In the real application the counties have a collection of cities and the cities have a collection of zipcodes. So the zipcodes need to have foreign keys to cities and cities to counties etc. If I use only composite ids I will end up with lots of extra columns in these tables (right?) that I would like to avoid. So thats why I wanted to use surrogate keys.
But I could agree to use composite-ids in Hibernate if there is a way to have another column generated by a generator (which the reffering tables could use as key)?
Is there maybe a way to use select-before-update and specify a select sql? ;)

A_gura: if I understand you right I have to iterate through each level of collection and select the entries from db first? Since there are so many objects I would like to avoid that. But if I do that what is the pragmatic way of updating each object? move id or propeties use evic etc?

Thanks,
William


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 6:03 am 
Newbie

Joined: Fri Apr 16, 2004 9:27 am
Posts: 18
Location: Russia, Spb
d99-vsp wrote:
A_gura: if I understand you right I have to iterate through each level of collection and select the entries from db first? Since there are so many objects I would like to avoid that. But if I do that what is the pragmatic way of updating each object? move id or propeties use evic etc?


No. Iteration is too expensive. Simple method is query execution with EXIST clause.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 8:44 am 
Newbie

Joined: Fri Feb 25, 2005 10:51 am
Posts: 8
Quote:
No. Iteration is too expensive. Simple method is query execution with EXIST clause


I'm not sure I understand. Could you explain or provide some pseudo code?
Lets say I want to save or update a state object which contains a set of county objects.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 9:07 am 
Newbie

Joined: Fri Apr 16, 2004 9:27 am
Posts: 18
Location: Russia, Spb
Problem:
You have few transient instances of County. You want to save them as dependent objects of State. You might be add all County objects to your State object and then call save() for State, but there is problem with unique constraint.

Decision:
Save your Counties separately from State entity.

1. Check, that County with same name doesn't exist in DB. For example, you may use count for it.
Code:
select count(county.id) from County county
where county.name = 'County_name'


Count work fast and doesn't load any objects. If result value equals zero, then no County object with given name in database (it's new county).

2. Set reference to State into County instance and store them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 9:25 am 
Newbie

Joined: Fri Feb 25, 2005 10:51 am
Posts: 8
Ok thanks,
That's what I though then I have to loop through each county object when I save a state.
First I thought there was a way to get cascade to work. I thought maybe I could specify two ids something like this:

Code:
If primary id is null
  then select using secondary id (unique constraint/combined-id)
    if match is found update
    else save


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