Hello, I'm working with JPA and Hibernate, and project requirement is to have primary key (id) value in application, before entity is persisted. JPA and Hibernate have techniques for generating id's during persist, but I need those id's before, and I need them to be unique. Jun 26, 2011 The thing is, while saving an object into the database, the generator informs to the hibernate that, how the primary key value for the new record is going to generate; hibernate using different primary key generator algorithms, for each algorithm internally a class is created by hibernate for its implementation. The JPA specification supports 4 different primary key generation strategies that generate the primary key values programmatically or use database features, like auto-incremented columns or sequences. The only thing you have to do is to add the @GeneratedValue annotation to your primary key attribute and choose a generation strategy. Sap b1 license key generator free.
Greenhorn
posted 14 years ago
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
Bartender
posted 14 years ago
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 ] Foreign Key
Greenhorn
![]() ![]()
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
Bartender
posted 14 years ago
Hibernate Primary Key No Generator Code
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. Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |