Home > database >  JPA EntityManager After update spring-boot-starter-parent from 2.1.3 to 2.3.11. Failed to initialize
JPA EntityManager After update spring-boot-starter-parent from 2.1.3 to 2.3.11. Failed to initialize

Time:04-15

I updated spring-boot-starter-parent from 2.1.3 to 2.3.11 in parent pom. Then the error occurs when I run the code: 2022-04-13 18:19:16.668 ERROR 24960 --- [ main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name= KeycodeAudit]

Caused by: org.hibernate.MappingException: The increment size of the [S_AUDITID] sequence is set to [50] in the entity mapping while the associated database sequence increment size is [1]. at org.hibernate.id.enhanced.SequenceStyleGenerator.configure(SequenceStyleGenerator.java:261) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final] at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final]

I'm new to spring and maven. Clueless about what causing it. If I change the spring-boot-starter-parent from 2.3.11 back to 2.1.3. Then the code can run again.

my JPA setting in application.yml is jpa: properties: hibernate.dialect: org.hibernate.dialect.Oracle10gDialect

Is that caused by missing schema? Or something else?

CodePudding user response:

I think the error message has suggested a validation error that allocationSize of sequence you specified in your entity class doesn't match the one in your db.

I'm not sure how you're configuring the entity class (by xml or using annotations), but if you're using annotations, you should configure your id field in following pattern:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "<seq name in java in generator anno>")
@SequenceGenerator(name = "<seq name in java in generator anno>", sequenceName = "<seq name in db>", allocationSize = 1) // Pay attention to here
private Long oid;

Please pay attention to allocationSize entry in SequenceGenerator. If you look into its source code, you may found it optional because it has provided default value "50".

    /**
     * (Optional) The amount to increment by when allocating 
     * sequence numbers from the sequence.
     */
    int allocationSize() default 50;

This entry corresponds to increment by when you define a sequence in Oracle. You should mannually specify it to 1, and that may be the root cause of your problem.

  • Related