Resolution of the random problem of Spring Data's self-increment ID in Oracle

Source: Internet
Author: User
Tags generator

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.