• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

insert expression as primary key, taking advantage of generator class mappings?

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not even sure if this is possible, my use case is as described.

I have a table, say Book, and it has a primary key say book_id which is of varchar2 type. The values for this column are of the format [F|N][0-9]*[DDMMYYYYHHMISS] format. ie. say example, F4358622032011123423 or N5358622032011123423, etc. where the first part is provided by the application (F stands for fiction, N stands for non-fiction) , middle numeric part is actually database sequence generated number and the third part is system timestamp.

I can map the middle part using the following mapping:



however, is there any possible way for me to delegate the creation of the entire expression(as described above) to hibernate itself. Is there a way for hibernate to evaluate an expression and add as a sequence ( for ex: say just the sequence generated number+ date timestamp)

Currently i use an utility class to generate the expression and set the returned string value to the field bookId of the Book entity. And my current mapping is as below:



I would appreciate any help regarding this.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you can implement your own version of org.hibernate.id.IdentifierGenerator and have it do the work for you inside hibernate.
 
Stanley Walker
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Emanuel, i will try out your solution and get back to you.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd caution against PKs with meaning. These have the irritating characteristic of needing changed as requirements change. Any reason you don't just use a surrogate key and define this as a distinct property? Then you can do whatever you like without having to write your own key generator.
 
Stanley Walker
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I was trying out stuff on hibernate. Am learning hibernate, so the more i pick up the more questions come into mind. This was one such case.
Anyways i had no idea i could extend and create my own identifier generator. Definitely would try it out.
But i have to agree with you, meaningful primary keys may change
 
    Bookmark Topic Watch Topic
  • New Topic