-->
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.  [ 15 posts ] 
Author Message
 Post subject: Hibernate Id Generator for SQL Server & Oracle 9i
PostPosted: Tue Sep 14, 2004 11:09 am 
Newbie

Joined: Tue Sep 14, 2004 10:57 am
Posts: 12
Hi folks,

I'm a nooby with Hibernate so bear with me... We have a model classes, beans if you like, that have the hibernate xdoclet tags to generate the xml mapping file. And we are planning to re-use these models for multiple projects. But recently a client requested that SQL Server was to be used instead of Oracle. So without keeping two versions of the model files is there a way to have the Id Generators work for both database types??

I know that sequence is Oracle and identity is MSSQL. I also saw native that hibernate would take care of which ID Gen to use, but how does that work? With sequence, you have to pass in a parameter for the sequece name, how can I do that with native? Or is there another way?

Thanks a bunch in advance

Jay


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 11:36 am 
Regular
Regular

Joined: Wed Aug 18, 2004 5:16 am
Posts: 69
Location: Modena, Italy
In oracle Hibernate will use as default a sequence named HIBERNATE_SEQUENCE, but I dont know if you can use the sequence parameter


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 11:59 am 
Newbie

Joined: Tue Sep 14, 2004 10:57 am
Posts: 12
But how will that work if I have more than one sequece? I cant have one sequence generate IDs for 20 tables, it will be kinda wierd and a total mess.


Teg wrote:
In oracle Hibernate will use as default a sequence named HIBERNATE_SEQUENCE, but I dont know if you can use the sequence parameter


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 12:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Why would that be a total mess, these are surrogate keys without any business meaning.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 12:18 pm 
Newbie

Joined: Tue Sep 14, 2004 10:57 am
Posts: 12
michael wrote:
Why would that be a total mess, these are surrogate keys without any business meaning.



I think my clients will frown upon having tables with

Table A
aID b c
1 a 23
5 x 67

Table B
bID d c
2 f 85
4 y 34

Table C
cID d e
3 h 9
6 o 4


Would anyone want this type of data? I'm sorry but thats just not clean to me.

-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 12:36 pm 
Newbie

Joined: Tue Sep 14, 2004 10:57 am
Posts: 12
hmmmm.... check this out!!

Can I use the generator class 'native' and still leave the param tag for the sequence name..?

-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 1:46 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
jaygarala wrote:
Can I use the generator class 'native' and still leave the param tag for the sequence name..?


I have done this with Oracle, and it worked.

_________________
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 3:39 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
An other approach would be to prepocess the mapping files and patch the id tag before feeding them to Hibernate.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 5:48 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Try this: http://www.hibernate.org/170.html...

We had exactly the same requirements as yours and build this ID generator. It works like a charm and should provide exactly what you are asking for ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 4:07 pm 
Newbie

Joined: Tue Sep 14, 2004 10:57 am
Posts: 12
I tried this but it didnt work. It tried to use the sequence in MS SQL, and ofcourse if spit out a big exception.

I dont understand this logic!!!

*** This block will never execute ***
// -- max_lo -> max_lo
if( ! SequenceHiLoGenerator.MAX_LO.equals(TableHiLoGenerator.MAX_LO) )
{
String value = params.getProperty(SequenceHiLoGenerator.MAX_LO);
if( value != null ) params.setProperty(TableHiLoGenerator.MAX_LO, value);
}

*** This block will always execute ***
// -- sequence name -> table name
if( ! SequenceHiLoGenerator.SEQUENCE.equals(TableHiLoGenerator.TABLE) )
{
String value = params.getProperty(SequenceHiLoGenerator.SEQUENCE);
if( value != null ) params.setProperty(TableHiLoGenerator.TABLE, value);
}

thanks,


-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 4:39 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
jaygarala wrote:
I tried this but it didnt work. It tried to use the sequence in MS SQL, and ofcourse if spit out a big exception.


post your exception then...

Quote:
I dont understand this logic!!!
*** This block will never execute ***


And why wouldn't be executed ?
I can tell you this piece of code works perfectly with SQLServer and Oracle since Hibernate 2.x

Maybe you haven't configure it correctly (in the ID part of your class mapping - see example at the bottom of the wiki page).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 16, 2004 9:54 am 
Newbie

Joined: Tue Sep 14, 2004 10:57 am
Posts: 12
The sequence is not the table name!! What do I change to handle that?

-Jay

jaygarala wrote:
I tried this but it didnt work. It tried to use the sequence in MS SQL, and ofcourse if spit out a big exception.

I dont understand this logic!!!

*** This block will never execute ***
// -- max_lo -> max_lo
if( ! SequenceHiLoGenerator.MAX_LO.equals(TableHiLoGenerator.MAX_LO) )
{
String value = params.getProperty(SequenceHiLoGenerator.MAX_LO);
if( value != null ) params.setProperty(TableHiLoGenerator.MAX_LO, value);
}

*** This block will always execute ***
// -- sequence name -> table name
if( ! SequenceHiLoGenerator.SEQUENCE.equals(TableHiLoGenerator.TABLE) )
{
String value = params.getProperty(SequenceHiLoGenerator.SEQUENCE);
if( value != null ) params.setProperty(TableHiLoGenerator.TABLE, value);
}

thanks,


-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 16, 2004 3:24 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
jaygarala wrote:
The sequence is not the table name!! What do I change to handle that?


... the sequence is not the table name...
What does this means ?

Does it mean the name of your sequence is not the same as the name of the table where the sequence values will be used ?

Then, as in the wiki page:
Code:
<id name="uid" column="id" type="long">
   <generator class="be.modulo1.framework.hibernate.id.NativeHiLoGenerator">
      <param name="sequence">idgen_alarm</param>
      <param name="max_lo">100</param>
   </generator>
</id>


Where the 'sequence' attribute refers to:
- if sequences are supported, it refers to the sequence name
- if not (fallback to hilo), it refers to the table holding the hi values

If the sequence name cannot be the same as the hilo tablename, then... write another generator yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 16, 2004 3:27 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
BTW: try to use "could you please" or "thank you" sometimes in your replies... It may help to get an answer.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 16, 2004 4:50 pm 
Newbie

Joined: Tue Sep 14, 2004 10:57 am
Posts: 12
ROFL... sorry I forget my manners when im working like crazy... and thanks for the response....

I figured it out... yeah!!! :D

Thanks for the Gen, probably will come in handy in the future...


-Jay


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