• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String as primary key in Hibernate

 
Hari patnaik
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have table in the database (sqlserver) with varchar data type as primary key.

I want to know what to use as primary key generatec class in the tablename.hbm.xml file.

Like in case of numeric columns we use identity or hilo what will be an equivalent for String (varchar).

I tried using uuid.hex but that generates a huge id & my column size is only 16.

I have seen some thing like generated class - assigned. but never found any implementation example for the same

Do post your views


Bye
Hari
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uuid.hex is the only generation strategy which returns a String. If you can't use this then for varchar primary keys your other options are is to use an assigned key, rather than a generated one, or to write your own IdentifierGenerator implementation.

However, if your PK is a relatively meaningless String (such as uuid.hex would generate) why not change its type to int or long and use one of the other generation modules?
[ May 13, 2005: Message edited by: Paul Sturrock ]
 
Hari patnaik
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

Thanks for your prompt response.

Wish I could follow your suggestion. But i do not have the liberty to change the column type of the table as lot of other apps also use the database

I have a requirement of generating a alpha numeric data (unique values) for this column & the data type of the column is String.

I want some sort of generator class equivalent to the above which I can use in Hibernate xml to map

If you see a solution do post it


Bye
Hari
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.

You'll need to decide how your generation algorithm is going to work, and write your own IdentifierGenerator implementation which uses it. Remember Hibernate is open source, so I'd suggest looking at how they implement the other IdentifierGenerators and see if they give you some ideas.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic