Hibernate 6 addScalar 用于将 json 列映射到 pojo

问题描述 投票:0回答:1

我正在尝试从 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
的包装。
然而,在 Hibernate 6 中,
.addScalar("jsonColumn", new JsonBinaryType(TestTabel.SomeJson.class))
不再起作用。
有解决方法或真正的解决方案吗?
jsonColumn 的类型为
jsonb
(PostgreSQL)

entitymanager spring-boot-3 hibernate-native-query hibernate-6.x hibernate-types
1个回答
0
投票

我已设法修复它。
而不是使用

.addScalar("jsonColumn", new JsonBinaryType(TestTabel.SomeJson.class))

你必须使用:

.addScalar("someJson", new CustomType<>(
     new JsonBinaryType(TestTabel.SomeJson.class), new TypeConfiguration()))
© www.soinside.com 2019 - 2024. All rights reserved.