where does spring storage store the id?

From the Hibernate documentation for identifier generators:

AUTO (the default)

Indicates that the persistence provider (Hibernate) should choose an appropriate generation strategy.

You didn’t list GenerationType as one of the annotations present, so it would default to AUTO. From the documentation for how AUTO works:

If the identifier type is numerical (e.g. Long, Integer), then Hibernate is going to use the IdGeneratorStrategyInterpreter to resolve the identifier generator strategy. The IdGeneratorStrategyInterpreter has two implementations:

FallbackInterpreter

This is the default strategy since Hibernate 5.0. For older versions, this strategy is enabled through the hibernate.id.new_generator_mappings configuration property. When using this strategy, AUTO always resolves to SequenceStyleGenerator. If the underlying database supports sequences, then a SEQUENCE generator is used. Otherwise, a TABLE generator is going to be used instead.

Postgres supports sequences, so you get a sequence. From a bit farther down in the same document:

The simplest form is to simply request sequence generation; Hibernate will use a single, implicitly-named sequence (hibernate_sequence) for all such unnamed definitions.

Hibernate asks Postgres to create a sequence. The sequence keeps track of what ids have been handed out, the database persists this internally. You should be able to get into the admin UI of the database and reset this sequence if you want.

To clarify, a database sequence is a database object independent of any tables (multiple tables can use the same sequence), so in general dropping a table won’t affect any sequences. The exception is when you’re using auto-increment, in which case there is an ownership relationship, and the sequence implementing the auto-increment is reset when the table is dropped.

It’s a judgment call on Hibernate’s part whether to make the default implementation of id generation use a sequence directly or auto-increment. If it used auto-increment you would see the values get recycled like you expected, but with the sequence there is no automatic reset.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top