-->
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.  [ 4 posts ] 
Author Message
 Post subject: Re-Persisting a formerly persistent object
PostPosted: Tue Nov 15, 2005 2:14 pm 
Newbie

Joined: Tue Nov 15, 2005 1:54 pm
Posts: 6
Hello,

I am using hibernate-3.1 and hibernate-annotations-3.1beta6 within a spring container (not as part of a servlet). I have the following situation:

Object x has been made persistent in Database A. The object is very simple, just a few fields and no associations at all. It's id-field is a long value.

Database A is no longer accessible, but I know the entire content of object x from a log-file (including the primary key). I want to create an exact copy in database B (exact meaning that even the id should be the same). Object x is not present in database B yet (there is no row with the same primary key).

I have tried both

session.merge(x);
session.replicate(x,ReplicationMode.OVERWRITE);

In either case, a new row is written successfully into the database, but the primary key is not the same. However, as I am using this primary key externally for other processes, this causes a problem for me.

I am still very new to Hibernate (I have only used direct JDBC and Versant so far), so I was hoping that anyone could give me a "push" into the right direction ;-) There are several paths I could follow, but I do not really like any of them:

    * I could create my own id-generator (which then could consequently manipulate to return the desired id). But why disabling standard hibernate code that works perfectly fine for me in 95% of all cases? Chances are that my code would be less stable and probably less performant.
    * I could refactor my system not to use this id-field any more. Besides the fact that it means a lot of work, I just hate the fact that I would have a field that has no valid use in my POJOs besides persistence.
    * I could create a second id-field just for Hibernate. Same concern as above. :-(


Since I am still very inexperienced regarding Hibernate, there is probably a better and easier way to do this. Could anyone show it to me, please? Your help is appreciated.

Best regards,

Jens

_________________
............................................

Jens Scheffler
Senior Software Engineer
INIT Innovations in Transportation, Inc.

1400 Crossways Blvd. Suite 110
Chesapeake, VA 23320, USA


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 5:08 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
I'd say roling your own id generator is the best approach in this situation. It isn't that dificult. Simply take a copy of the one you are using now and make sure it's using the given id if it's already set.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 9:51 am 
Newbie

Joined: Tue Nov 15, 2005 1:54 pm
Posts: 6
Quote:
I'd say roling your own id generator is the best approach in this situation.


Hmm... Sounds like the least of the evils. Thanks, I will try that.

One thing is not quite clear to me, though: is it general policy not
to do anything with the object id? I would imagine that, as soon as
people use the id-field in their applications, having it changed by Hibernate might cause trouble...

_________________
............................................

Jens Scheffler
Senior Software Engineer
INIT Innovations in Transportation, Inc.

1400 Crossways Blvd. Suite 110
Chesapeake, VA 23320, USA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 4:37 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Your absolutely right. Leave the id to hibernate and the id generator.

Sometimes the id looks very handy to solve some problems easily but sooner or later you'll face problems. Maybe you have to change the id generator or objects are saved in different order and do not yet have a valid id, just to name a few.

HTH
Ernst


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