PooledOptimizer ID generation returns used values

Description

When attempting to persist an entity through EntityManager.persist, the sequence generator returns an old value. This causes a constraint violation and makes this version unusable for us. The field is defined as follows:

Within the database the value of the sequence is around 130, but the JPA attempts to persist it with an id that around 80 (both numbers increase on each attempt).

As far as I've been able to trace the problem, it comes from org.hibernate.id.enhanced.PooledOptimizer lines 88 and 89

Upon first firing of generate(), it sets the value to be 49 behind the actual sequence. A possible fix could be

However I haven't looked if the rest of the implementation keeps the value properly in sync with the database.

P.S. we are still using a custom build of 4.3.10 with 3 fixes to bypass the problems preventing us from using the official versions. Two other problems that impacted us have been fixed so far. I hope 5.0.9 will finally allow us to finally return to the official builds.

Environment

Hibernate 5.0.8
Apache Tomcat 7.0.65
PostgreSQL 9.4.6

Assignee

Unassigned

Reporter

Йордан Гигов

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Minor
Configure