[hibernate-dev] Negative sequence numbers

Gail Badner gbadner at redhat.com
Tue Apr 18 21:35:10 EDT 2017


I see a comment on HHH-10219 that sounds like negative sequence values are
supported by NoopOptimizer [1], but it does not seem to work.

I've pushed a test case to my fork [2] with an entity defined as:

    @Entity( name = "TheEntity" )
    @Table( name = "TheEntity" )
    public static class TheEntity {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"ID_GENERATOR")
        @SequenceGenerator(name = "ID_GENERATOR", sequenceName =
"ENTITY_SEQ", initialValue= -10, allocationSize = -1)
        public Integer id;
    }

The first generated value is -10; the second generated value should be -11,
but it is -9.

The sequence is exported as:

create sequence ENTITY_SEQ start with -10 increment by 1

In the debugger, I can see that NoopOptimizer is being used, which has the
correct incrementSize (-1).

The problem is that SequenceStructure#getSourceIncrementSize always returns
1. This is because SequenceStructure#applyIncrementSizeToSourceValues gets
initialized to false because NoopOptimizer#applyIncrementSizeToSourceValues
returns false. [3]

If I change NoopOptimizer#applyIncrementSizeToSourceValues to return true,
then the test passes. Unfortunately, SequenceHiLoGeneratorNoIncrementTest
fails though because Hibernate tries to create a sequence that increments
by 0.

If I define NoopOptimizer#applyIncrementSizeToSourceValues as follows, both
my test [2] and SequenceHiLoGeneratorNoIncrementTest pass.

        public boolean applyIncrementSizeToSourceValues() {
               return getIncrementSize() != 0;
        }

Should Hibernate support negative sequence values?

If so, is my proposed fix OK?

Regards,
Gail

[1]
https://hibernate.atlassian.net/browse/HHH-10219?focusedCommentId=73362&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-73362
[2] https://github.com/gbadner/hibernate-core/tree/negative-sequence-values
[3]
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStructure.java#L162-L164


More information about the hibernate-dev mailing list