简体   繁体   中英

when migrating from hibernate 5.2 to 5.3, hibernate tries to use a weird sequence

After the upgrade from hibernate 5.2.17 to 5.3.6, i get this error :

Caused by: org.h2.jdbc.JdbcSQLException: Schéma "ENHANCED" non trouvé
Schema "ENHANCED" not found; SQL statement:
call next value for enhanced.SequenceStyleGenerator [90079-197]
    at org.h2.engine.SessionRemote.done(SessionRemote.java:623) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.CommandRemote.prepare(CommandRemote.java:85) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.CommandRemote.<init>(CommandRemote.java:51) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.engine.SessionRemote.prepareCommand(SessionRemote.java:493) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1247) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:304) ~[h2-1.4.197.jar:1.4.197]
    at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:567) ~[c3p0-0.9.5.2.jar:0.9.5.2]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87) ~[hibernate-core-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172) ~[hibernate-core-5.3.6.Final.jar:5.3.6.Final]

the id field is annotated this way :

@Id
@GeneratedValue(generator = "enhanced.SequenceStyleGenerator")
@GenericGenerator(
    name = "enhanced.SequenceStyleGenerator", 
    parameters = {
            // this value needs to be used when creating the sequence in "increment-by" clause.
            @Parameter(name = "increment_size", value= "10"),
            // default name : hibernate_sequence
            @Parameter(name = "prefer_sequence_per_entity", value= "false"),
            @Parameter(name = "optimizer", value="pooled")
    },
    strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator")
@Column(name = "ID", nullable = false)
private Long id;

With hibernate 5.2 it is working as expected, but not anymore with hibernate 5.3.

The migration guide here : https://github.com/hibernate/hibernate-orm/blob/5.3/migration-guide.adoc do not reference any change on sequence generator.

What could be the issue ?

I've found that it is a new behavior of hibernate 5.3 :

In the method SequenceStyleGenerator.determineSequenceName that code was added :

final Boolean preferGeneratorNameAsDefaultName = serviceRegistry.getService( ConfigurationService.class )
            .getSetting( AvailableSettings.PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME, StandardConverters.BOOLEAN, true );
if ( preferGeneratorNameAsDefaultName ) {
    final String generatorName = params.getProperty( IdentifierGenerator.GENERATOR_NAME );
    if ( StringHelper.isNotEmpty( generatorName ) ) {
        fallbackSequenceName = generatorName;
    }
}

The new default behavior is to use the generator name as sequence name. So there are two possibilities to migrate from hibernate 5.2 to 5.3:

  • change the generator name to be the sequence name
  • revert to the hibernate 5.2- behavior of not using the generator name by setting hibernate.model.generator_name_as_sequence_name to false in hibernate config (or the generator parameters)

Your Generator Name is interpreted as belonging to a specific schema, renaming should fix this - avoid the dot.

Please check the manual Section 2.6.11 on how to name and parametrize generators: https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM