Hibernate 5.1 migration Sequence generator issue

Description

Hi, I am trying to migrate from 4.3.6.Final to 5.1.0 Final to get java 8 date time api mapping and unrelated join entities features.
But unfortunately I got an issue related with sequence generation.
Here is my test:

CREATE SEQUENCE c_seq start 1 increment 1;
---------------------------------------------------------------------------------------------------------------------
Console output:

Actually you have changed sequence strategy generation in new 5.1.0 version
old: SequenceHiLoGenerator
new: SequenceStyleGenerator
And SequenceStyleGenerator uses PooledOptimizer.
I've spent time debugging PooledOptimizer and got that it happens due to this piece of code

But it works fine with Hibernate 4.3.6.Final
Console output:

As we can see everything is fine, allocationSize works and there is no exception with 4.3.6.
I am really wondering about negative ids and this issue.
NOTE: If I change my sequence to CREATE SEQUENCE c_seq start 1 increment 20;
,increment size is the same as allocationSize, it works fine:

It even better then in 4.3.6 cause id starts from 1, not from 20

Environment

DB: PostgreSql

Activity

Show:
Vlad Mihalcea
July 22, 2016, 7:17 PM
Edited

That's because Hibernate 4 uses old identifier generators. You can read more about it in this article. The reason why it fails is that the old generator used hilo, while the new one uses pooled.

To switch to the old identifier generators, all you need to do is supply the following property:

It's all documented in the 4.3 -> 5.0 Migration Guide:

Vlad Mihalcea
July 23, 2016, 5:02 AM

This is not an issue, but just an observation of an intended change.

Igor Dmitriev
July 23, 2016, 12:28 PM
Edited

Thx Vlad, I've already known about this flag and it should be enabled to false, but it didn't work for me due to wrong order of this methods invocation:

entityManagerFactory.afterPropertiesSet();
entityManagerFactory.setJpaPropertyMap(jpaProperties());

but it should be vice versa, come on. But thanks this bug I've learned how sequence generator works

Vlad Mihalcea
July 23, 2016, 12:35 PM

You're welcome.

Assignee

Unassigned

Reporter

Igor Dmitriev

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure