• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Single row/column database table to hold an auto-incremented counter with Hibernate?  RSS feed

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to keep a counter in a database which I can use for computing a value in my Java code. Each time I use the counter value I want to have it auto-incremented, that way if another thread reads the counter value it will be sure to be unique. I want to use Hibernate to do this, but it's not obvious to me how I would go about setting it up. Can anyone give me some suggestions on how I would go about it?

My first thoughts are to have an entity class which has a single field "counter", and a corresponding DAO class which has a single method getCounterValue() which will return the current counter value which will be incremented in the corresponding table. Does this seem reasonable?

Thanks in advance for any suggestions.

--James
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a class/mapping that just has the id defined with an increment key-generation strategy. Then you can create and save an instance of this class each time.

You might be able to piggyback off of the versioning support in Hibernate as well.
 
James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestion Javid. I have implemented a solution which takes this approach and it's working well. I appreciate your help!

--James
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!