Giter VIP home page Giter VIP logo

Comments (8)

jfairley avatar jfairley commented on May 26, 2024

We are beginning to play with JDBI in some new projects. (The goal is to dump the heavy ORM we have been using.) Do you have any ideas or thoughts on using Immutables with JDBI?

from immutables.

elucash avatar elucash commented on May 26, 2024

I've done some prototyping in January to reuse marshalers as a binding engine (by generating JsonTokens out of result set and feeding them to marshaler), this requires no additional annotation processing. Some straightforward generation of Mapper is also a choice. As there's renewed interest in this, I'll try to find that prototype to share, and will think about Mapper generation in a meantime.

from immutables.

jfairley avatar jfairley commented on May 26, 2024

I found that simply using the @GenerateGetters annotation on my immutable classes is enough to allow me to use them with JDBI.

The one thing that didn't work was property binding, because the JDBI code assumed getFoo() existed for property foo.

Now that my immutables have getters generated, the following JDBI method works perfectly:

@Nullable
@SqlQuery("SELECT * FROM user WHERE id = :id")
User findById(@Bind("id") long id);

from immutables.

elucash avatar elucash commented on May 26, 2024

Wow, that is great! BTW Does it works both ways?

This weekend I found the earlier prototype, but it's not very suitable, because I had to deviate from JDBI mapper interface to something very custom — it was based on result set meta data and JSON marshaling with token buffer.

I still planning to create straightforward mapper generation, just want to get to first milestone of new template/processing engine.

from immutables.

jfairley avatar jfairley commented on May 26, 2024

No, I'm using a mapper. I actually went straight to building my own mapper. I assumed it wouldn't work without a mapper since the immutable relies on the builder rather than a constructor or individual setters.

more example...

@GenerateGetters
@GenerateImmutable
public abstract class User {
    @GenerateDefault
    public long id() {
        return -1;
    }

    public abstract String firstName();
    public abstract String lastName();
}


@RegisterMapper(UserMapper.class)
public interface UserRepository {
    @GetGeneratedKeys
    @SqlUpdate("INSERT INTO user (first_name, last_name) VALUES (:firstName, :lastName)")
    long insert(@BindBean User user);

    @Nullable
    @SqlQuery("SELECT * FROM user WHERE id = :id")
    User findById(@Bind("id") long id);
}


public class UserMapper implements ResultSetMapper<User> {
    @Override
    public User map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException {
        return User.builder()
                .id(resultSet.getLong("id"))
                .firstName(resultSet.getString("first_name"))
                .lastName(resultSet.getString("last_name"))
                .build();
    }
}

from immutables.

elucash avatar elucash commented on May 26, 2024

Oh, I see. So you can expect mappers like UserMapper soon will be generated using new annotation: both ways, without reflection

from immutables.

jfairley avatar jfairley commented on May 26, 2024

👍

from immutables.

elucash avatar elucash commented on May 26, 2024

Basic integration implemented and released as part of 1.0.
I choose to stick to marshaling bridge rather than separate code generation. The main problem which I avoided to solve by this is creation of separate system for plugging extension types, therefore just reused JSON marshalers.
I would like to hear your feedback as I did only basic testing.
See usage example here

from immutables.

Related Issues (20)

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.