Search code examples
javahibernatejpajboss7.xjpa-2.0

Hibernate generates negative id values when using a sequence


I have a class with the following definition:

@Id
@SequenceGenerator(name = "SEQ_ACE_WORKERS_QUEUE_STATS_ID", sequenceName = "SEQ_ACE_WORKERS_QUEUE_STATS_ID", allocationSize = 500)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACE_WORKERS_QUEUE_STATS_ID")
@Column(name = "ID")
private long Id;

When we ran it on Jboss 4.2.3 it worked fine and generated the proper ID's (starting from 1000+)

Now we moved to jboss 7.1.1 and it generates negative ID's! (starting from -498 and going up)

Any idea why this might happen?


Solution

  • I just ran into this issue when migrating from JBoss 6.1 to JBoss 7.1.

    According to the JBoss AS 7.1 JPA documentation ( https://docs.jboss.org/author/display/AS71/JPA+Reference+Guide#JPAReferenceGuide-Persistenceunitproperties),

    JBoss 7.1 automatically sets several hibernate properties. One of the properties being set is hibernate.id.new_generator_mappings which activates new ID generators that use different algorithms and are not backwards compatible. Setting this property to false in your persistence.xml file will restore the old ID generator behavior.

    The hibernate 4 documentation also has information regarding the new ID generators: http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html_single/#mapping-declaration-id-generator.

    The hibernate documentation clearly states that the new ID generators are not enabled by default, but, as noted above, JBoss 7.1 is automatically enabling them.