Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate Id.

 
pankaj semwal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi List[],

I am doing the project and need to generate the id for each record.I am using oracle as back end.Can body tell me how to generate unique id.


Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Oracle you can use sequences. Have a read of the chapter on sequences in the Oracle documentation.

The downside to using sequences is you introduce a database dependency. If this is a concern, have a google. for "primary key generation patterns" for some database neutral solutions.
 
pankaj semwal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is any method in java by which we can generate long id's .If yes den
please help me.
 
Sachin Telang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you want java method/ you can create your own method not more than 2-5 lines. you can use any collection classes to keep in unique.
[ November 26, 2008: Message edited by: Sachin Telang ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pankaj semwal:
Is any method in java by which we can generate long id's .If yes den
please help me.


Unique within the JVM? Yes, see java.lang.UUID. Unique within a database? No. The only thing that can guarentee this is the database itself.


Why you want java method/ you can create your own method not more than 2-5 lines. you can use any collection classes to keep in unique.

Again, this solution suffers from the problem that it can only be unique within your program, not unique within the database.
[ November 26, 2008: Message edited by: Paul Sturrock ]
 
pankaj semwal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any body write the code for doing this.


Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pankaj semwal:
Hi,
Can any body write the code for doing this.


Thanks


Please be aware that JavaRanch is NotACodeMill. What have you tried? Have you read the Oracle documentation on sequences? Have you googled for "primary key generation patterns"? Have you read the javaDocs for java.lang.UUID and considered the downsides of using this with a database?
[ November 26, 2008: Message edited by: Paul Sturrock ]
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't want to be database dependant (don't want to use oracle sequences) but need the id to be cross application, there is the old fasioned sequences_table solution
where you have a table and each row in the table contains:
the table that needs an id, and the next available id for that row.
you then simply selelct that value, add 1 and insert it back. (obviously locking the row before doing this)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic