-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can we use id if present and generate one if not present?
PostPosted: Wed Mar 09, 2005 12:35 pm 
Beginner
Beginner

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 20
Location: USA
Hi all,
Say, I have a table CAT with a primary key CAT_ID.

I need to auto generate CAT_ID using:
<generator class="native"/> OR <generator class="identity"/>

But if I have a value for CAT_ID in the object, I don't want a generated id.

I am using session.save(newCat) to persist the object 'newCat'.

How do I go about doing this?

Thanks a lot.
Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 12:41 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
You may only specify one generator.

If you already have a identifier in an object you shouldn't be saving it - you should be updating it.

but...

I'm not sure if i understand your question. If you have a value for catId in the object does it already exist in the database?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 12:49 pm 
Beginner
Beginner

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 20
Location: USA
Hi drj,
This is actually for 2 different environments. Both environments will have seperate copies of code and database.

In one environment 'Development', all objects will be inserted with the generated IDs.

Now, when I am inserting selected data from 'Development' to another environment 'Test', they should carry the same IDs as in 'Development'.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 1:46 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
If they are separate environments then I assume they will have separate hbm.xml files.
You could use the generated identity in one environment (dev) and the other generator in the non-development environment but you would need to be aware of the implications of this.

I'm beginning to question the use of hibernate in this situation. Is this is bulk transfer of data?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 2:03 pm 
Beginner
Beginner

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 20
Location: USA
Hi drj,
The same version of code needs to reside on both the environments.

Is it possible to determine during runtime what *.hbm.xml file will be used? Say, have something like CatDevelopment.hbm.xml and CatTest.hbm.xml ? I can determine what environment I am in by a Java environment varaible. Will this help in deciding what *.hbm.xml file to use?

We are not performing bulk transfer. We have used hibernate in other modules of the project; so just maintaining consistency.

I have set up the tables in such a way that it will auto-generate id's if not present. For this I have used the GENERATE BY DEFAULT IDENTITY feature in DB2. Since DB2 does it, I was wondering why not Hibernate?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 4:58 pm 
Beginner
Beginner

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 20
Location: USA
The solution is the Session.replicate() method in Hibernate 3.0

Here's a snippet from the Hibernate Reference Manual (10.9):
Quote:
It is occasionally useful to be able to take a graph of persistent instances and make them persistent in a different datastore, without regenerating identifier values.

Code:
//retrieve a cat from one database
Session session1 = factory1.openSession();
Transaction tx1 = session1.beginTransaction();
Cat cat = session1.get(Cat.class, catId);
tx1.commit();
session1.close();

//reconcile with a second database
Session session2 = factory2.openSession();
Transaction tx2 = session2.beginTransaction();
session2.replicate(cat, ReplicationMode.LATEST_VERSION);
tx2.commit();
session2.close();




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