entitymanagerspring-boot-3hibernate-native-queryhibernate-6.xhibernate-types

Hibernate 6 addScalar for mapping json column to a pojo


I am trying to migrate from SpringBoot 2 to 3.
Previously, with the help of hibernate types we could run native queries and map the result to a POJO like so:

        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);

Note: dbService is just a wrapper over EntityManager.
In Hibernate 6 however, .addScalar("jsonColumn", new JsonBinaryType(TestTabel.SomeJson.class)) is not working anymore.
Is there a workaround or a real solution?
jsonColumn is of type jsonb (PostgreSQL)


Solution

  • I have managed to fix it.
    Instead of using

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

    you have to use:

    .addScalar("someJson", new CustomType<>(
         new JsonBinaryType(TestTabel.SomeJson.class), new TypeConfiguration()))