wood burning stoves 2.0*
The moose likes JDBC and the fly likes Unique primary key generator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Unique primary key generator" Watch "Unique primary key generator" New topic
Author

Unique primary key generator

Dmitry Shekhter
Greenhorn

Joined: Feb 21, 2001
Posts: 26
Hi,
For the project i'm working on, i need to insert data to fill
the tables in the database schema, what i'm trying to figure out is what is an efficient way to generate unique primary keys for each of the tables, as each table has to have a unique primary key, but that data is not going to be provided to my database bean... These primary don't have to be meaningful, they are just to uniquely identify each record and support relationships between the tables in the schema.
I have some ideas, but i was hoping that someone who has already went through this would share the pattern.
Thanks in advance,
Dmitry
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Many databases have database-specific ways to do this. SQL Server can autonumber primary keys. Oracle has sequences to generate numbers. Etcetera.
If you don't want to be database-specific, use some type of service to generate these numbers (can be a class, bean, EJB, RMI or Corba callable service, whatever) in conjunction with a "nextnum" table. For best efficiency, if strictly consecutive numbers are not necessary, you grab a range of numbers from the nextnum table and only go back to the database once you've exhausted that range. With this technique you can have multiple, distributed number generators.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unique primary key generator
 
Similar Threads
what if no ID column in the table for Hibernate?
how to make data retrieval / query faster
B&S 2.1.2 Record number
can i use one entity bean to access more than one tables
Multiple Tables In the Database -- Use Multiple Entity Beans?