• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
James Adams
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
 
Javid Jamae
Author
Ranch Hand
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic