aspose file tools*
The moose likes Object Relational Mapping and the fly likes autogenerated keys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "autogenerated keys" Watch "autogenerated keys" New topic
Author

autogenerated keys

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi,
I want to know how hibernate generates key values like primary key. For oracle database does it use some sequence.


Groovy
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This depends on your key generation strategy. See the Hibernate documentation for the various possibilities.

In Oracle it can use a sequence, if you choose to configure it that way. Straying of topic a little, using a sequence is not necessarily a good idea, since it ties you into databases that support them (or some other such mechanism). Better is to use some database agnostic algorithm (e.g. a High Low Key Generation pattern) and an assigned key.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Probably the easiest way it to just leave the job up to your database. Simply putting the @GeneratedId annotation on your primary key will allow Hibernate to delegate key generation to your database.



This tutorial discusses various primary key generation strategies, including sequence, tablegenerator and identity strategies.

Primary Key Generation Strategies with Hibernate and JPA Annotations

-Cameron McKenzie
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Paul
Can you tell me how database agnostic algorithm would generate unique keys. It could generate duplicate key right? Does it use machine timestamp . There could be problem if the time is reset.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi author

For the auto generation strategy does hibernate create sequence or table automatically ?

Thanks
Prad
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Hibernate has three strategies for key generation: identity, sequence and table.



You can read more about them here through this little tutorial I put together:

Unique Primary Key Generation Strategies with Hibernate and JPA Annotations

The AUTO option just uses the database default, which is typically just identity. Sequence and table provides a little bit more flexibility to the way ids are generated.

-Cameron McKenzie
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Cameron,
What is the default for oracle database ?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I believe auto is the default for all of them.

Most of the databases I use default to Identity. I haven't confirmed this with Oracle yet, though.

-Cameron McKenzie
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: autogenerated keys