-->
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: Fabricated ID Generator
PostPosted: Tue May 30, 2006 11:08 am 
Newbie

Joined: Tue May 30, 2006 10:27 am
Posts: 1
Hi,
This is my first time on this forum. Hope to get some solution to a small problem I am facing with Hibernate.

I have a Oracle table (lets say T) whose primary key is, lets say, P. While storing records in this table I use the <generator> for "P", which uses oracle's "sequence". This works fine.

In this table, I have to add another column, lets say Q. Although this new column will hold unique values for each record (and behaves like a primary key), yet it should not to be part of any key in "T". So far so good.
But my problem starts now:
1. Column Q should be "sequence" driven. I have created a new sequence for this. But I don't know how to use this sequence in the <property>. I am not sure if two sequence generators (one for P and one for Q) are allowed in one mapping xml. I need help here.

2. Column Q's final value should be a "function" of this new sequence. This new sequnce will return a numeric value (everytime unique). Using this unique number I have to create a unique string and then store that string value in the Q column. Again, I don't know how to do this and need help.
note: I have the logic available for this function. All I need to know is "can I", and "how to" use it in hbm mapping document ?

Thanks,
Sumit


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 12:43 pm 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
1. where's your sequence stored?

2. Would you be better off having a get method which returned the generated sequence value and set it as a normal property if it's based on another value? You could do the (painful) 'if not null then generate else return' for new or current entries, so it only calculates for new objects

obvious issue is you won't have an id for a new object. I have come across the DefaultSaveOrUpdateEventListener object in hibernate which allows you to run code as the save occurs, but whether you'd be able to get a handle on the id at that point is another matter that i'm afraid i can't answer without looking into it.

However, going on the assumption that your new unique value cannot be null, i don't think you'll ever have a handle on the id value until after the object has been saved, by which point your second unique key will still be null.

I'm assuming you have a good reason (or a need) to have two unique keys rather than one? If it's purely for use within the code rather than the db, would it not be easier to generate it in the code when your code needs it, as at that point you would have your unique id available?


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.