Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft for jackson mapping of a row onto an object #183

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

rainbowdashlabs
Copy link
Owner

@rainbowdashlabs rainbowdashlabs commented May 25, 2024

Draft for mapping a row onto a POJO using jackson.

Creation of a mapper supporting Jackson

        // Create a Registry for mapping the types of a database to java objects.
        RowMapperRegistry registry = new RowMapperRegistry().register(PostgresqlMapper.getDefaultMapper());
        // Wrap the database registry into a jackson registry
        JacksonRowMapperRegistry jacksonRegistry = new JacksonRowMapperRegistry(new ObjectMapper().findAndRegisterModules(), registry);
        // Register the jackson registry at the query configuration
        QueryConfiguration.setDefault(QueryConfiguration.builder(dc)
                .setRowMapperRegistry(jacksonRegistry)
                .build());

The java object

public record TestObj(int first, long second, String third, Instant fourth, UUID fifth) {
}

Quick and quite simple poc test

query("INSERT INTO test(first, second, third, fourth, fifth) VALUES (?,?,?,?,?::UUID)")
        .single(call().bind(obj.first()).bind(obj.second()).bind(obj.third()).bind(obj.fourth(), INSTANT_TIMESTAMP).bind(obj.fifth(), UUID_STRING))
        .insert();

var result = query("SELECT first, second, third, fourth, fifth::UUID FROM test")
        .single()
        .mapAs(TestObj.class)
        .first()
        .get();

Assertions.assertEquals(obj, result);

@rainbowdashlabs rainbowdashlabs changed the base branch from dev to feature/value-reader May 25, 2024 13:54
@rainbowdashlabs rainbowdashlabs force-pushed the feature/jackson-mapper branch from 2b406df to 7e60670 Compare May 25, 2024 13:54
Base automatically changed from feature/value-reader to dev May 25, 2024 14:51
@rainbowdashlabs rainbowdashlabs force-pushed the feature/jackson-mapper branch from 88cf440 to 24cbb4e Compare May 25, 2024 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant