尝试设置 r2dbcCustomConversions 时出现“找不到 PersistentEntity”映射异常。
我遵循了r2dbc 参考文档中的一些代码片段
这是我的代码:
@Configuration
class ServiceConfiguration : AbstractR2dbcConfiguration() {
@Bean
override fun connectionFactory() =
ConnectionFactories.get("CONNECTION_STRING")
override fun r2dbcCustomConversions() = R2dbcCustomConversions(
storeConversions,
listOf(PersonReadConverter(), PersonWriteConverter())
)
}
@ReadingConverter
class PersonReadConverter : Converter<Row, Person> {
override fun convert(source: Row): Person {
return Person(
source.get("id", String::class.java),
source.get("name", String::class.java),
source.get("age", Int::class.java)
)
}
}
@WritingConverter
class PersonWriteConverter : Converter<Person, OutboundRow> {
override fun convert(source: Person): OutboundRow? {
val row = OutboundRow()
row["id"] = SettableValue.from(source.id!!)
row["name"] = SettableValue.from(source.name!!)
row["age"] = SettableValue.from(source.age!!)
return row
}
}
@Table
data class Person(@Id val id: String?, val name: String?, val age: Int?)
@Service
class PersonService(private val databaseClient: DatabaseClient) : InitializingBean {
override fun afterPropertiesSet() {
selectAll()
.subscribe(
{ println("Data: $it") },
{ println("Error: $it") },
{ println("Done") }
)
}
fun selectAll() = databaseClient
.select()
.from(Person::class.java)
.fetch()
.all()
}
这是我得到的输出:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in file [\demo\reactive-web\target\classes\com\example\demo\PersonService.class]: Invocation of init method failed; nested exception is org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type class com.example.demo.Person!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1783) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:868) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:67) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) [spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:404) [spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) [spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1275) [spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1263) [spring-boot-2.2.0.M3.jar:2.2.0.M3]
at com.example.demo.DemoApplicationKt.main(DemoApplication.kt:13) [classes/:na]
我注意到文档有点过时了。例如,使用 SettableValue 所需的 PersonWriteConverter 示例。我不确定这是一个错误还是我遗漏了什么