DEV Community

Yu Han
Yu Han

Posted on

JDBC Mapping Without Reflection: Up to 117 Faster Than Spring JDBC

SQL execution isn't the only cost of a database query. Turning a ResultSet into Java objects costs time and memory, too.

What if we generated the mapping code at compile time instead?

That's what SJF4J does with @CompiledJdbcMapper.

A simple example

Define a Java record:

public record User(long id, String userName, int age) {}
Enter fullscreen mode Exit fullscreen mode

Declare a mapper:

@CompiledJdbcMapper
public interface UserMapper {
    UserMapper INSTANCE = CompiledInstances.create(UserMapper.class);

    List<User> users(ResultSet rs);
}
Enter fullscreen mode Exit fullscreen mode

That's it. Given a JDBC ResultSet:

List<User> users = UserMapper.INSTANCE.users(rs);
Enter fullscreen mode Exit fullscreen mode

Column names such as user_name are automatically matched to userName.

No XML. No handwritten mapping loops. No per-row reflection.

How fast is it?

We benchmarked SJF4J against Spring JDBC and MyBatis using a 12-column result set.

Here are the results for 1,000 rows, fully materialized into a List:

Target vs. Spring JDBC vs. MyBatis Explicit
POJO 14.32× 10.57×
Record 117.47× —
Map 2.48× 4.92×

For Java records, SJF4J also allocated 50× less memory than Spring's DataClassRowMapper.

The benchmark uses JMH, JDK 25, and H2 on an AMD EPYC 7763 runner. It measures result-set traversal and object mapping, not SQL execution or network I/O.

Full benchmark results and methodology

Why is it faster?

Instead of discovering how to map objects at runtime, SJF4J generates specialized Java code at compile time.

At runtime, it:

  • Resolves column indexes once.
  • Reads values using typed JDBC getters.
  • Creates objects directly.

The generated code is much closer to handwritten JDBC mapping than a general-purpose reflective mapper.

Prefer to control the row loop yourself?

Prepared row mapping is also supported:

@CompiledJdbcMapper
public interface UserMapper {
    JdbcRowMapper<User> prepare(ResultSetMetaData metadata);
}
Enter fullscreen mode Exit fullscreen mode
JdbcRowMapper<User> mapper = CompiledInstances
        .create(UserMapper.class)
        .prepare(rs.getMetaData());

while (rs.next()) {
    User user = mapper.map(rs);
    // Process the user
}
Enter fullscreen mode Exit fullscreen mode

Column resolution happens once, not for every row.

Just mapping, not another ORM

SJF4J does not replace your SQL framework or manage your database connections. It simply makes the ResultSet-to-object step faster.

Keep your SQL. Keep your JDBC code. Compile the mapping.

Feedback and contributions are welcome!

Top comments (4)

Collapse
 
userxnom profile image
Abdullah Naseer •

The 117.47x number for records is the headline, but the 14.32x for POJOs against Spring JDBC is the one that convinced me — that is the shape most existing code actually has, and it still shows a huge gap from generating the mapping at compile time instead of discovering it per row. Resolving column indexes once and using typed JDBC getters is exactly the handwritten-JDBC pattern, just generated. The question I would want answered before adopting it is what happens when the mapping is not clean: a nullable column, a custom type, or a column name that does not match userName. Does that fail at compile time, or does it fall back to something slower at runtime?

Collapse
 
hannyu profile image
Yu Han •

Great question! There are three different cases:

  • Nullable columns: SQL NULL maps to Java null for reference types. Mapping NULL to a primitive throws an exception rather than silently returning zero.
  • Column names: Case-insensitive and snake_case matching are supported by default. Explicit mapping is also available: @Mapping(target = "userName", source = "account_name").
  • Custom types: SJF4J supports custom ValueCodec implementations, which can be called directly from the generated code.

Invalid mapping definitions are rejected at compile time where possible. Missing or ambiguous columns are detected when resolving the actual ResultSet metadata.

Some custom type conversions may use runtime conversion, but the entire mapper does not fall back to reflection-based mapping.

The goal is to keep the mapping explicit, predictable, and as close to handwritten JDBC as possible.

Collapse
 
tedk profile image
Ted Kupolov •

Compile-time generation is a neat idea. One suggestion: Spring's DataClassRowMapper is on the slower side of reflective mappers, so the 117x figure is partly a function of the baseline. Adding Jdbi (ConstructorMapper/RecordMapper) and a handwritten RowMapper would make the comparison more realistic, since Jdbi resolves column bindings once per ResultSet rather than per row. It would also help to show how much of end-to-end latency mapping accounts for once real SQL and network I/O are involved. An in-memory H2 benchmark isolates the part that's easiest to win.

Disclosure: I work on CoilBase, which sits on Jdbi. In our whole-stack benchmarks (PostgreSQL, two rounds, also including Quarkus and Micronaut Data), Jex + CoilBase started about 2x faster than Spring Boot + Spring JDBC and used roughly a third less peak RSS, with p99 about on par. That's a full-stack comparison, and much of the gap likely comes from the HTTP framework rather than mapping, but it's the kind of end-to-end view I'd love to see for SJF4J too.

Collapse
 
hannyu profile image
Yu Han •

Thanks for the suggestion! I've added Jdbi to the benchmark, and the results look quite good. As you pointed out, Jdbi's RowMapper has a one-time specialization cost per ResultSet, which is amortized over larger result sets, unlike Spring JDBC's per-row mapping overhead.

Here are the updated results:
github.com/sjf4j-projects/sjf4j/bl...

CoilBase looks like a great project! Both CoilBase and SJF4J share a lightweight philosophy, and I think they could complement each other quite well.

Since SJF4J focuses on individual operations such as mapping and validation rather than being an application framework, its benchmarks intentionally isolate those operations. But given that CoilBase is a lightweight alternative to Spring, it might be interesting to integrate SJF4J into CoilBase and include it in your end-to-end benchmarks.

Thanks again for the valuable feedback. I'd love to keep the conversation going!