Comments (8)
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.
I've done some prototyping in January to reuse marshalers as a binding engine (by generating JsonToken
s 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.
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.
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.
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.
Oh, I see. So you can expect mappers like UserMapper
soon will be generated using new annotation: both ways, without reflection
from immutables.
👍
from immutables.
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)
- allow builder method and protected constructor HOT 1
- allow Check to use private interface methods
- builder should return interface/abstract HOT 1
- Not passing through Bean Validation constraints on containers
- feature: constructor method that returns builder HOT 1
- Can I create a builder for an existing class that I don't control? HOT 1
- Custom Nullable annotations produce compilation errors HOT 1
- implementation type not visible when using @Criteria and ImplementationVisibility.PACKAGE
- @Value.Default
- How to reducing annotation clutter?
- Using strict builders gives Jackson deserialize failures
- Generated 'from' method has trouble with parameterized interfaces that declare properties HOT 2
- Issue with nested quotes in strings when using passAnnotations HOT 1
- Underriding toString will select invalid static method in preference to valid default method HOT 1
- cannot find annotations noise
- xor options HOT 2
- from method HOT 2
- "Sandwich" style immutables in combination with Jackson and Set-type properties leads to compile time warnings in generated code HOT 1
- passAnnotations patterns HOT 1
- Better Support for Jackson's @JsonSubTypes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from immutables.