作为学习 r2DBC 的一部分,我遇到了枚举转换面临的问题。我在这里使用 PostgreSQL。在读取评级为PG-13 和 NC-17(任何带有破折号的内容)的 Film 数据时,我遇到了问题。
下面是我的表格架构
create table film
(
film_id integer default nextval('film_film_id_seq'::regclass) not null
constraint film_pkey
primary key,
title varchar(255) not null,
description text,
release_year year,
language_id smallint not null
constraint film_language_id_fkey
references language
on update cascade on delete restrict,
rental_duration smallint default 3 not null,
rental_rate numeric(4, 2) default 4.99 not null,
length smallint,
replacement_cost numeric(5, 2) default 19.99 not null,
rating mpaa_rating default 'G'::mpaa_rating,
last_update timestamp default now() not null,
special_features text[]
);
mpaa_rating 定义为
create type mpaa_rating as enum ('G', 'PG', 'PG-13', 'R', 'NC-17');
这是我在我的配置中注册转换器的代码
@Configuration
@EnableTransactionManagement
@EnableR2dbcRepositories
@EnableR2dbcAuditing
public class DVDRentalDBConfiguration extends AbstractR2dbcConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
System.out.println("Initializing postgreSQL connection factory");
return new PostgresqlConnectionFactory(
PostgresqlConnectionConfiguration.builder()
.host("localhost")
.database("dvdrental")
.username("postgres")
.password("postgres")
.codecRegistrar(EnumCodec.builder().withEnum("mpaa_rating", Rating.class).build())
.build()
);
}
@Override
protected List<Object> getCustomConverters() {
return Collections.singletonList(new RatingWritingConverter());
}
@Bean
ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) {
System.out.println("Initializing postgreSQL connection factory");
return new R2dbcTransactionManager(connectionFactory);
}
}
我的检索代码非常简单
private Mono<FilmModel> getFilmById(Long id) {
return filmRepository.findById(id).switchIfEmpty(Mono.error(DataFormatException::new));
}
添加抛出的异常https://gist.github.com/harryalto/bd51bbcdd081868c5064c808d08205e4
我尝试研究堆栈溢出,但无法找出问题所在。任何帮助是极大的赞赏。