We all know the default behaviour of Hibernate when using @SequenceGenerator
- it increases real database sequence by one, multiple this value by 50 (default allocationSize
value) - and then uses this value as entity ID.
This is incorrect behaviour and conflicts with specification which says:
allocationSize - (Optional) The amount to increment by when allocating sequence numbers from the sequence.
To be clear: I do not bother about gaps between generated IDs.
I care about IDs that are not consistent with underlying database sequence. For example: any other application (that e.g. uses plain JDBC) may want to insert new rows under IDs obtained from sequence - but all those values may be already used by Hibernate! Madness.
Do somebody know any solution to this problem (without setting allocationSize=1
and thus degrading performance)?
EDIT:
To make things clear.
If last inserted record had ID = 1
, then HB use values 51, 52, 53...
for its new entities BUT at the same time: sequence's value in database will be set to 2
. Which can easily leads to errors when other applications are using that sequence.
On the othe hand: specification says (in my understanding) that database sequence should have been set to 51
and in the meantime HB should use values from range 2, 3 ... 50
UPDATE:
As Steve Ebersole mentioned below: the behaviour described by me (and also the most intuitive for many) can be enabled by setting hibernate.id.new_generator_mappings=true
.
Thanks all of You.
UPDATE 2:
For future readers, below you can find a working example.
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USERS_SEQ")
@SequenceGenerator(name = "USERS_SEQ", sequenceName = "SEQUENCE_USERS")
private Long id;
}
persistence.xml
<persistence-unit name="testPU">
<properties>
<property name="hibernate.id.new_generator_mappings" value="true" />
</properties>
</persistence-unit>
To be absolutely clear... what you describe does not conflict with the spec in any way. The spec talks about the values Hibernate assigns to your entities, not the values actually stored in the database sequence.
However, there is the option to get the behavior you are looking for. First see my reply on Is there a way to dynamically choose a @GeneratedValue strategy using JPA annotations and Hibernate? That will give you the basics. As long as you are set up to use that SequenceStyleGenerator, Hibernate will interpret allocationSize
using the "pooled optimizer" in the SequenceStyleGenerator. The "pooled optimizer" is for use with databases that allow an "increment" option on the creation of sequences (not all databases that support sequences support an increment). Anyway, read up about the various optimizer strategies there.