Hibernate version:3.1
<id name="id" type="long" unsaved-value="null">
<column name="DataId" />
<generator class="org.hibernate.id.MultipleHiLoPerTableGenerator">
<param name="table">tblSequenceTable</param>
<param name="primary_key_column">SequenceName</param>
<param name="value_column">NextVal</param>
<param name="primary_key_value">tblData</param>
<param name="max_lo">1000</param>
</generator>
</id>
Name and version of the database you are using:SQL Server 2005
I use hibernate to generate primary keys using the org.hibernate.id.MultipleHiLoPerTableGenerator class. With a NextVal value of 15974085 in the tblSequenceTable, the ids are being generated as a negative value. Seems like the org.hibernate.id.MultipleHiLoPerTableGenerator cannot handle values greater than Integer.MAX_VALUE, even though the javadoc says
Quote:
A hilo IdentifierGenerator that returns a Long, constructed using a hi/lo algorithm
.
Looking at the source for this class, the generate() method can only return values in the Integer range as it casts the value returned from the doWorkInCurrentTransaction() method to an integer. If the hival is greater than Integer.MAX_VALUE then this method will return a random number which is the result of truncating the size to an integer.
I was able to resolve this issue by changing the Integer to Long where applicable in the MultipleHiLoPerTableGenerator class.
I think the javadoc for this class is somewhat misleading and it should also mention that the class cannot handle values greater than Integer.MAX_VALUE (even though the returned value is Long when the type of the id is long).
A better solution is for MultipleHiLoPerTableGenerator to be able to generate Long ids greater than Integer.MAX_VALUE without truncating.