One of my field (not the ID) need to have a unique value (int smaller than 10,000). I decide to write sequence in the DB and get this field value from the sequence , can hibernate help me to do this in generic way?
You can access sequences for purposes other than ids by mapping the sequence as a named sql query. Something like:
However, if you are using sequences your solution is no longer generic (since it doesn't work on databases that don't support sequences). You will also run out of numbers long before you hit your 10,000 limit because sequences don;t guarentee to increment by one.
All that aside, if you have a unique, unchanging property of an entity that is not null, why not make it part of your primary key?