Giter VIP home page Giter VIP logo

rosetta's Introduction

Rosetta Build Status

Overview

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:

  1. Binds Java objects to SQL query parameters
  2. Maps SQL result sets to Java objects

Usage

To use with jDBI on Maven-based projects, add the following dependency:

<dependency>
  <groupId>com.hubspot.rosetta</groupId>
  <artifactId>RosettaJdbi</artifactId>
  <version>3.5</version>
</dependency>

Binding

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.

Mapping

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 { /* ... */ }

Configuration

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, you can create a file located at
src/main/resources/META-INF/services/com.fasterxml.jackson.databind.Module
containing 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

rosetta's People

Contributors

jhaber avatar ldriscoll avatar tdavis avatar tpetr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.