I have problem with MSSQL ID jumping feature.
My Requirement is like,I need to generate a sequence number say,starting from 1000 and increment one by one and my application is hosted in multiple servers and points to same DB.Multiple host are balanced with Nginx.
For this, wrote an Entity Class with SequenceGenerator
@Entity
@SequenceGenerator(name = "IdSequence",
sequenceName = " ID_GEN",
allocationSize = 1)
public class Example implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = " IdSequence ")
private long id ;
}
And it works fine. When I restarted the DB, the next ID is jumped to current+1000
To resolve this I add a hibernate property, and its work fine against the ID jumping.
<property name="hibernate.id.new_generator_mappings" value="true"/>
The real problem now is already existing entities with Auto generation strategy has getting exception like
Cannot insert explicit value for identity column in table ‘USER' when IDENTITY_INSERT is set to OFF.
- Is there any way to resolve the ID jumping issue in JPA/Hibernate
- How can I avoid the
IDENTITY_INSERT is set to OFF
if am going with same fix.
Could you please suggest a better option to generate sequence number that should be unique.
Thanks in advance