1

I use Spring Data JDBC with an Oracle database and I set a custom id on my entity with a BeforeSaveEvent processor.

@Bean
public DataFieldMaxValueIncrementer incrementer() {
    OracleSequenceMaxValueIncrementer incrementer = new OracleSequenceMaxValueIncrementer();

    incrementer.setDataSource(metahubDatasource());
    incrementer.setIncrementerName("TL_SEQ");

    return incrementer;
}

@Bean
public ApplicationListener<?> idSetting() {

    return (ApplicationListener<BeforeSaveEvent>) event -> {

        if (event.getEntity() instanceof TL) {
            setIds((TL) event.getEntity());
        }
    };
}

private void setIds(TL tl) {
    tl.setId(incrementer().nextLongValue());
}

When I try to persist this entity the exception below is thrown.

  org.springframework.dao.DataRetrievalFailureException: The generated key is not of a supported numeric type. Unable to cast [oracle.sql.ROWID] to [java.lang.Number]
    at org.springframework.jdbc.support.GeneratedKeyHolder.getKey(GeneratedKeyHolder.java:79)
    at org.springframework.data.jdbc.core.DefaultDataAccessStrategy.getIdFromHolder(DefaultDataAccessStrategy.java:323)
    at org.springframework.data.jdbc.core.DefaultDataAccessStrategy.insert(DefaultDataAccessStrategy.java:111)
    at org.springframework.data.jdbc.core.DefaultJdbcInterpreter.interpret(DefaultJdbcInterpreter.java:73)
    at org.springframework.data.relational.core.conversion.DbAction$InsertRoot.doExecuteWith(DbAction.java:110)
    at org.springframework.data.relational.core.conversion.DbAction.executeWith(DbAction.java:55)
    ...

How can I configure the generated key column to avoid this exception or how can I configure that no generated key is expected?

1 Answers1

0

The problem seems to be that Oracle behaves yet again differently than the other databases used in integration tests of Spring Data JDBC.

There is work underway to include Oracle in the integration tests. Once that is available it will either directly fix this issue or allow to fix it.

In the meantime probably the only thing you can do is to patch Spring Data JDBC yourself by tweaking DefaultDataAccessStrategy.getIdFromHolder to make it not fail when there is no id.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348