I have an entity defined with a sequence-generated primary key:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_key_gen")
@SequenceGenerator(name = "id_key_gen", sequenceName = "id_key_seq")
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}
I'm using PostgreSQL, and this key is defined as a serial. According to PostgreSQL
select last_value from id_key_seq;
returns
1603.
When I do a create() to persist an instance of this entity, I'm seeing the following in my logs (unrelated stuff edited out):
05 15:15:26.948 org.hibernate.id.enhanced.SequenceStructure [DEBUG] - Sequence value obtained: 1604
05 15:15:26.948 org.hibernate.event.def.AbstractSaveEventListener [DEBUG] - generated identifier: 1554, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator
Subsequent SQL insert statements refer to the value 1554, not what it should be using, 1604 (based on the value returned from the SequenceStructure. Where did Hibernate get 1554 from?
Seems to me Hibernate has a bug here - SequenceStructure knows the correct next value, but it isn't being used. Any idea how to resolve this?
FYI: I'm aware of this page, which says to use GenerationType.AUTO because the "Hibernate folks completely messed this up", but there isn't much beyond that not-very-helpful statement.
Looks like if you use GenerationType.SEQUENCE, you need to be specifying an "increment value" of 1 to avoid it using the sequence as a Hi/Lo seed.
The first answer (the useful one) to the question you posted explains that you need to specify "allocationSize=1" in the @GeneratedValue annotation.
On newer Hibernate releases you can instead set hibernate.id.new_generator_mappings=true
in your Hibernate properties; see the docs.
来源:https://stackoverflow.com/questions/4609461/hibernate-generating-two-different-sequence-ids-for-postgresql-insert