1. Introduction to the development environment
Spring Data 1.6, JDK 1.6, Oracle 11g, Hibernate 4.3.5
2. The question was raised
In development, you use annotation to configure the entity, which is the mapping of the object to the table, and the code example is as follows:
@Entity (name = "Payorderentity") @Table (name = "Es_outpay_order") public class OrderEntity extends Auditablebaseentity {@ Id@column@generatedvalue (strategy = generationtype.sequence, generator = "Ordergenerator") @SequenceGenerator (name = " Ordergenerator ", Sequencename =" outpay_order_sequence ") private long ID; ......}
Random IDs are inserted into the database, with an increment of ID 50, 150, 1050, and the like:
2. Problem analysis
The database itself has been analyzed, no problem, working properly; the creation of a database table is also verified without problems, and finally, it is analyzed to determine the problem that should be the code. Originally, using trigger to define the ID field of the self-increment, now use annotation in the sequencegenerator, a new problem.
Is it a matter of generator definition itself? After inspection and verification, no problem.
Or search the Internet, and finally see an unused field allocationsize.
3. Resolution of the problem
The code after the fix is as follows:
@Entity (name = "Payorderentity") @Table (name = "Es_outpay_order") public class OrderEntity extends Auditablebaseentity {@ Id@column@generatedvalue (strategy = generationtype.sequence, generator = "Ordergenerator") @SequenceGenerator (name = " Ordergenerator ", Sequencename =" Outpay_order_sequence ", allocationsize=1) private long ID; ........}
4. What is Allocationsize?
In the source code definition, the default value is 50, so the ID of 50/51 appears. In JPA, its ID is set to change size by allocationsize.
Resources:
1. Http://stackoverflow.com/questions/12745751/hibernate-sequencegenerator-and-allocationsize
2. http://royontechnology.blogspot.hk/2010/04/note-on-allocationsize-parameter-of.html
Resolution of the random problem of Spring Data's self-increment ID in Oracle