3

I got the following error below. I try to find where I did it wrong between Long and Int but not found anywhere any idea?

Log Error

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initializer' defined in class path resource [com/abc/checkout/agentoperator/config/H2Configuration.class]: Invocation of init method failed; nested exception is org.springframework.r2dbc.connection.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [schema.sql]: CREATE TABLE IF NOT EXISTS agents ( id SERIAL PRIMARY KEY, store_number INTEGER, country VARCHAR(5), name VARCHAR(255), status VARCHAR(10), last_updated TIMESTAMP ); nested exception is java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap')
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.22.jar:5.3.22]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.22.jar:5.3.22]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66) ~[spring-boot-2.7.3.jar:2.7.3]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.3.jar:2.7.3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.3.jar:2.7.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.3.jar:2.7.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.3.jar:2.7.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.3.jar:2.7.3]
    at com.abc.checkout.agentoperator.AgentOperatorApplicationKt.main(AgentOperatorApplication.kt:12) ~[classes/:na]
Caused by: org.springframework.r2dbc.connection.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [schema.sql]: CREATE TABLE IF NOT EXISTS agents ( id SERIAL PRIMARY KEY, store_number INTEGER, country VARCHAR(5), name VARCHAR(255), status VARCHAR(10), last_updated TIMESTAMP ); nested exception is java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap')
    at org.springframework.r2dbc.connection.init.ScriptUtils.lambda$runStatement$9(ScriptUtils.java:571) ~[spring-r2dbc-5.3.22.jar:5.3.22]
    at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.MonoStreamCollector$StreamCollectorSubscriber.onError(MonoStreamCollector.java:150) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.MonoStreamCollector$StreamCollectorSubscriber.onNext(MonoStreamCollector.java:137) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlatMap$FlatMapMain.tryEmitScalar(FluxFlatMap.java:488) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlatMap$FlatMapMain.onNext(FluxFlatMap.java:421) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:129) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxIterable$IterableSubscription.slowPath(FluxIterable.java:272) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxIterable$IterableSubscription.request(FluxIterable.java:230) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:171) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlatMap$FlatMapMain.onSubscribe(FluxFlatMap.java:371) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:96) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:165) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:87) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.Flux.subscribe(Flux.java:8466) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlatMap.trySubscribeScalarMap(FluxFlatMap.java:200) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlatMap.subscribeOrReturn(FluxFlatMap.java:93) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.Mono.subscribe(Mono.java:4382) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:451) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onNext(FluxConcatMap.java:251) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.drainAsync(FluxFlattenIterable.java:421) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.drain(FluxFlattenIterable.java:686) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.onNext(FluxFlattenIterable.java:250) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxHandleFuseable$HandleFuseableSubscriber.onNext(FluxHandleFuseable.java:191) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:299) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onNext(FluxFilterFuseable.java:337) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:160) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onComplete(FluxContextWrite.java:126) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxUsing$UsingConditionalSubscriber.onComplete(FluxUsing.java:549) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxCreate$BaseSink.complete(FluxCreate.java:460) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxCreate$BufferAsyncSink.drain(FluxCreate.java:805) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxCreate$BufferAsyncSink.complete(FluxCreate.java:753) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxCreate$SerializedFluxSink.drainLoop(FluxCreate.java:247) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxCreate$SerializedFluxSink.drain(FluxCreate.java:213) ~[reactor-core-3.4.22.jar:3.4.22]
    at reactor.core.publisher.FluxCreate$SerializedFluxSink.complete(FluxCreate.java:204) ~[reactor-core-3.4.22.jar:3.4.22]
    at org.springframework.core.io.buffer.DataBufferUtils$ReadCompletionHandler.completed(DataBufferUtils.java:986) ~[spring-core-5.3.22.jar:5.3.22]
    at org.springframework.core.io.buffer.DataBufferUtils$ReadCompletionHandler.completed(DataBufferUtils.java:917) ~[spring-core-5.3.22.jar:5.3.22]
    at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:129) ~[na:na]
    at java.base/sun.nio.ch.Invoker.invokeDirect(Invoker.java:160) ~[na:na]
    at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:188) ~[na:na]
    at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:300) ~[na:na]
    at java.base/sun.nio.ch.WindowsAsynchronousFileChannelImpl$ReadTask.completed(WindowsAsynchronousFileChannelImpl.java:477) ~[na:na]
    at java.base/sun.nio.ch.WindowsAsynchronousFileChannelImpl$ReadTask.failed(WindowsAsynchronousFileChannelImpl.java:485) ~[na:na]
    at java.base/sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:389) ~[na:na]
    at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:113) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
    Suppressed: java.lang.Exception: #block terminated with an error
        at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99) ~[reactor-core-3.4.22.jar:3.4.22]
        at reactor.core.publisher.Mono.block(Mono.java:1707) ~[reactor-core-3.4.22.jar:3.4.22]
        at org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer.execute(ConnectionFactoryInitializer.java:112) ~[spring-r2dbc-5.3.22.jar:5.3.22]
        at org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer.afterPropertiesSet(ConnectionFactoryInitializer.java:97) ~[spring-r2dbc-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.22.jar:5.3.22]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.22.jar:5.3.22]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.22.jar:5.3.22]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66) ~[spring-boot-2.7.3.jar:2.7.3]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.3.jar:2.7.3]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.3.jar:2.7.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.3.jar:2.7.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.3.jar:2.7.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.3.jar:2.7.3]
        at com.abc.checkout.agentoperator.AgentOperatorApplicationKt.main(AgentOperatorApplication.kt:12) ~[classes/:na]
Caused by: java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap')
    at java.base/java.util.stream.Collectors.lambda$summingLong$23(Collectors.java:699) ~[na:na]
    at reactor.core.publisher.MonoStreamCollector$StreamCollectorSubscriber.onNext(MonoStreamCollector.java:132) ~[reactor-core-3.4.22.jar:3.4.22]
    ... 47 common frames omitted

H2Configuration

@Configuration
class H2Configuration {

    @Bean
    fun initializer(connectionFactory: ConnectionFactory): ConnectionFactoryInitializer {
        val initializer = ConnectionFactoryInitializer()
        initializer.setConnectionFactory(connectionFactory)
        val populator = CompositeDatabasePopulator()
        populator.addPopulators(ResourceDatabasePopulator(ClassPathResource("schema.sql")))
        populator.addPopulators(ResourceDatabasePopulator(ClassPathResource("data.sql")))
        initializer.setDatabasePopulator(populator)
        return initializer
    }
}

AgentEntity

@Table("agents")
data class AgentEntity(

    @Id
    @Column("id")
    var id: Long?,

    @Column("store_number")
    var storeNumber: Int,

    @Column("country")
    var country: String,

    @Column("name")
    var name: String,

    @Column("status")
    var status: String,

    @Column("last_update")
    var lastUpdate: OffsetDateTime
)

AgentRepository

@Repository
interface AgentRepository : ReactiveCrudRepository<AgentEntity, Long> {
    @Query("SELECT * FROM agents WHERE status = :status")
    fun findAgentByStatus(status: String): Flux<AgentEntity>

    @Query("select * from agents where id = :agentId ")
    fun findByAgentId(agentId: Long): Flux<AgentEntity>
}

data.sql

INSERT INTO agents (id, store_number, country, name, status) VALUES (1, 5019, 'TH', 'devid', 'UP');
INSERT INTO agents (id, store_number, country, name, status) VALUES (2, 5019, 'TH', 'angle', 'DOWN');
INSERT INTO agents (id, store_number, country, name, status) VALUES (3, 5018, 'TH', 'nomad', 'UP');
INSERT INTO agents (id, store_number, country, name, status) VALUES (4, 5018, 'TH', 'iceberg', 'DOWN');

schema.sql

CREATE TABLE IF NOT EXISTS agents (
  id SERIAL PRIMARY KEY,
  store_number INTEGER,
  country VARCHAR(5),
  name VARCHAR(255),
  status VARCHAR(10),
  last_updated TIMESTAMP
);
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96
  • There is format mismatch `serial` uses `integer` not `long`. Either change data to integer or use `bigserial` in schema. – nicholasnet Sep 23 '22 at 17:00

1 Answers1

0

AFAIK if you use spring boot < 3.x, you should better use 0.x versions of R2DBC, in my experience the combination of spring boot 2.7.4 and Oracle R2DBC 1.0.0 leads to the same exception. Changing Oracle R2DBC from 1.0.0 to 0.4.0 fixed the problem.

Askar
  • 544
  • 1
  • 6
  • 17