Hi,
According documentation GenerationType.AUTO always does a fallback to either
Identity-, Sequence- or Hilo-generator
depending on the capabilities of the underlying database.
Following your comments I guess in your case it falls back to GenerationType.TABLE.
If true then on your database there must exist a table named hibernate_sequences with
following 2 columns:
sequence_name (varchar](255) NULL) and
sequence_next_hi_value (int NULL)
This table holds a hilo value for each table.
Quote:
When does hibernate get a new primary key ?
Hibernate gets the new primary key when you persist a new entity object.
Quote:
May I refresh the internal hibernate PK counters?
As I know this is not possible, unless you implement and use a own custom generator.
Quote:
Do I have to restart the persistanceUnit in some sort?
Yes. If in meantime you have altered the content of table hibernate_sequences outside hibernate
(for example within PDI) then you must close and restart the entitymanager factory
in order that the changed values in table hibernate_sequences are considered by hibernate
Code:
_entityManagerFactory.close();
_entityManagerFactory= javax.persistence.Persistence.createEntityManagerFactory(...);
An alternative approach would consist in using identity generator type which is supported by databases like DB2,MySQL,MS-SQL , Sybase ,HSQLDB etc. making the DB is responsible to assign the primary keys for new inserted objects.
Advantages of this approach:
-no matter about duplicate assigns or wrong set sequence because the db takes the full responsibility
-insertions of new records also feasible also outside hibernate application.
Disadvantages:
-only applicable with table per class-hierarchy mapping (aka flat-mapping).
-no deferring (batching) of jdbc statement possible on insertions because hibernate needs to know the assigned id immediately
(= extra jdbc round trip on makePersistent)
-You need to bootstrap your database again when switching between table and identity generator, because the schema changes.