我正在尝试从 SpringBoot 2 迁移到 3。
以前,借助 hibernate 类型,我们可以运行本机查询并将结果映射到 POJO,如下所示:
var sp = dbService.nativeQueryBuilder("select id, json_column as \"jsonColumn\" from test_table")
.unwrap(NativeQuery.class)
.addScalar("id", StandardBasicTypes.LONG) // LongType.INSTANCE for SpringBoot 2
.addScalar("jsonColumn", new JsonBinaryType(TestTabel.SomeJson.class))
.setResultListTransformer(new AliasToBeanResultTransformer(SomeJsonResult.class));
return dbService.executeAndReturnListResult(sp, SomeJsonResult.class);
注意:dbService 只是
EntityManager
的包装。.addScalar("jsonColumn", new JsonBinaryType(TestTabel.SomeJson.class))
不再起作用。jsonb
(PostgreSQL)
我已设法修复它。
而不是使用
.addScalar("jsonColumn", new JsonBinaryType(TestTabel.SomeJson.class))
你必须使用:
.addScalar("someJson", new CustomType<>(
new JsonBinaryType(TestTabel.SomeJson.class), new TypeConfiguration()))