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>
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…