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.
Rosetta isn't an ORM. It doesn't silently read and write to your database, validate input, or manage connections. It does two things:
- Binds Java objects to SQL query parameters
- Maps SQL result sets to Java objects
To use with jDBI on Maven-based projects, add the following dependency:
<dependency>
<groupId>com.hubspot.rosetta</groupId>
<artifactId>RosettaJdbi</artifactId>
<version>3.7</version>
</dependency>
You can bind JDBI arguments in your DAO using @BindWithRosetta
.
public interface MyDAO {
@SqlUpdate("UPDATE my_table "
+"SET some_field=:some_field, another_field=:another_field "
+"WHERE id=:id")
void update(@BindWithRosetta MyRow obj);
}
@BindWithRosetta
behaves like jDBI's @BindBean
, but it converts the object to a tree using Jackson which lets
you use all the Jackson annotations you know and love to customize the representation. It's also generally more robust - it supports the not-quite-standard naming conventions, fluent setters, nested objects (with dot-notation), getters without fields, etc.
To register Rosetta globally for mapping you can add it to 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 { /* ... */ }
Assuming your Java field names are camel-case and your SQL column names are lowercase with underscores (a pretty common scenario) you can make this work with Rosetta by annotating the Java objects with @RosettaNaming(LowerCaseWithUnderscoresStrategy.class)
which will change Jackson's naming strategy to lower case with underscores but only for Rosetta binding/mapping (other Jackson operations throughout your application are unaffected).
Or to make this change globally for Rosetta, you could make a Jackson Module
that sets the default naming strategy to lowercase with underscores, something like this:
public class LowerCaseWithUnderscoresModule extends SimpleModule {
@Override
public void setupModule(SetupContext context) {
context.setNamingStrategy(new LowerCaseWithUnderscoresStrategy());
}
}
You can then register it with Rosetta programmatically during your app's startup with something like:
Rosetta.addModule(new LowerCaseWithUnderscoresModule());
Or even better your module could extend com.hubspot.rosetta.databind.AutoDiscoveredModule
, you can create a file located at
src/main/resources/META-INF/services/com.hubspot.rosetta.databind.AutoDiscoveredModule
Rosetta also supports injecting from the default Module:
src/main/resources/META-INF/services/com.fasterxml.jackson.databind.Module
Both files should contain a newline separated list of fully qualified class names for the modules you want to load, in this case it would contain something like
your.package.LowerCaseWithUnderscoresModule