Rosetta is a Java library that leverages Jackson to take the pain out of mapping objects to/from the DB, designed to integrate seamlessly with Jdbi. Jackson is extremely fast, endlessly configurable, and already used by many Java webapps.
If you are on Jdbi 2, add the following dependency:
<dependency>
<groupId>com.hubspot.rosetta</groupId>
<artifactId>RosettaJdbi</artifactId>
<version>{latest version}</version>
</dependency>
Latest versions can be seen here
Or if you are on Jdbi 3, add the following dependency:
<dependency>
<groupId>com.hubspot.rosetta</groupId>
<artifactId>RosettaJdbi3</artifactId>
<version>{latest version}</version>
</dependency>
Latest versions can be seen here
You can bind JDBI arguments in your DAO using @BindWithRosetta
.
public interface MyDAO {
@SqlUpdate("UPDATE my_table SET name = :name, type = :type WHERE id = :id")
void update(@BindWithRosetta MyRow obj);
}
@BindWithRosetta
converts the object to a tree using Jackson, and then binds every property in the JSON tree on the Jdbi statement (using dot notation for nested object fields). This lets you use all the Jackson annotations you know and love to customize the representation.
With Jdbi 2, you can register the Rosetta mapper globally by adding it your DBI
like so:
dbi.registerMapper(new RosettaMapperFactory());
Or to test it out on a single DAO you would do:
@RegisterMapperFactory(RosettaMapperFactory.class)
public interface MyDAO { /* ... */ }
Or to use in combination with a Handle
: (same idea to register on a Query
)
handle.registerMapper(new RosettaMapperFactory());
With Jdbi 3, you can register the Rosetta mapper globally by adding it your Jdbi
like so:
jdbi.registerRowMapper(new RosettaRowMapperFactory());
Or to test it out on a single DAO you would do:
@RegisterRowMapperFactory(RosettaRowMapperFactory.class)
public interface MyDAO { /* ... */ }
Or to use in combination with a Handle
: (same idea to register on a Query
)
handle.registerRowMapper(new RosettaRowMapperFactory());
For a list of advanced features, see here