BeforeSaveEvent
我将 Spring Data JDBC 与 Oracle 数据库一起使用,并使用处理器在我的实体上设置了自定义 ID 。
@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());
}
当我尝试保留此实体时,会引发以下异常。
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)
...
如何配置生成的密钥列以避免此异常,或者如何配置不需要生成的密钥?