I decided to make use of a Hibernate id generator which meet the following requirements: - safe id generation when the domain is accessed from different applications(different JVMs) - make use of id intervals (do not query the database every time a new ID is needed)
After some investigations I choose one of 2 hibernate enhanced identifier generators, it's the
org.hibernate.id.enhanced.TableGenerator
The problem is that this algorithm keep in database not the next value available but the end of the next available interval, so, let's say I have an id generator with increment_size 10, when i make a request for an id I receive the interval 1 - 10, but in the database is now stored not the value 11, but 21. With this behavior I have to keep the increment_size the same along all classes that map to a specific table. Why does it have this behavior ? Is there any way to fix this ?
org.hibernate.id.enhanced.TableGenerator defines a table that is capable of generating multiple values simultaneously. Sounds like you try to have it generate identifiers from just a single value for multiple entities. This is controlled by the 'segment_value' TableGenerator config setting, if you wanted to take advantage of it.
And as for the values, there is nothing to "fix". Its not broken. If you want different behavior, configure different behavior. This is controlled by something called an optimizer, defined by the TableGenerator 'optimizer' config setting. This is all covered in the manual : http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#mapping-declaration-id See specifically the section "5.1.2.3. Enhanced identifier generators" and "5.1.2.3.1. Identifier generator optimization". The manual does not talk about all the available optimizers. Sounds like the one you want is called "pooled-lo", which is just like "pooled", but stores the lo value rather than the high value.