Hibernate Enhanced Identity Generators

I decided to use the Hibernate id generator, which meets the following requirements: - create a secure identifier when accessing the domain from different applications (different JVMs) - use identifier intervals (do not query the database every time a new identifier is required)

After some research, I choose one of two generators with an enlarged sleep generator, it

org.hibernate.id.enhanced.TableGenerator

The problem is that this algorithm does not store the next available value in the database, but the end of the next available interval, so let's say I have an id generator with increment_size 10, when I make an request for an identifier, I get an interval of 1 - 10. but the database now stores not the value 11, but 21. With this behavior, I must keep the increment_size value the same for all classes that map to a specific table. Why does this have such a behavior? Is there any way to fix this?

+4
source share
2 answers

org.hibernate.id.enhanced.TableGenerator defines a table that can generate multiple values ​​at once. It looks like you are trying to get it to generate identifiers from only one value for multiple objects. This is controlled by configuring the TableGenerator 'segment_value' configuration if you want to use it.

As for the values, nothing can be fixed. He did not break. If you need a different behavior, configure a different behavior. This is controlled by something called an optimizer, determined by the configuration settings of the TableGenerator optimizer. All this is described in the manual: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#mapping-declaration-id See Section "5.1.2.3. Advanced Identifier Generators" and " 5.1.2.3.1. Optimization of the identifier generator. " The manual does not talk about all available optimizers. It looks like the one you want is called "pooled-lo", which is similar to "pooled" but retains the value of lo, rather than a high value.

+4
source

This post is old, but for others that may have the same problem.

As Steve Ebersole pointed out, you should use the pooled-lo optimizer in your case. Check also your version of sleep mode, it should be> = 4.3.11, because in previous versions in issue .

To give some explanation, with the pooled-lo optimizer, the value stored in the database is the low value of the next available interval.

So, if the identifier of the last object is stored in [1; 10], the next available interval [11,20], and the value stored in the database will be 11, as expected.

Thus, if you have another program that does not use hibernate and does not even know the size of the increment defined in the sleep mode configuration, it will still be able to insert objects without breaking the sequence.

All you have to do is atomize the value of the sequence and increase it, and then use the value obtained (before "incrementation") as the new identifier of the object that it wants to insert. In our example, to insert one row, it will update the sequence value to 12 and add a new object with identifier 11. Thus, when hibernate reaches the last id of its current interval in memory (10), it will query the database and save the value 22, so that save a new id interval for it [12; 21].

+1
source

Source: https://habr.com/ru/post/1385310/


All Articles