Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

autogenerated keys

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to know how hibernate generates key values like primary key. For oracle database does it use some sequence.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron,
What is the default for oracle database ?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic