Using Spring JdbcTemplate Instead of Object-Relational Mapping

Home » Blog » Software » Using Spring JdbcTemplate Instead of Object-Relational Mapping

Spring JDBC and Spring JdbcTemplate in particular has become my go-to tool for accessing relational databases for quite some time now and for good reason in my opinion:

The common way for using data from an RDBMS in an object-oriented context used to be and for the most part still is object-relational mapping (ORM) frameworks like the Java Persistence API (JPA) or RailsActiveRecord. As the name suggests these frameworks attempt to map data (and concepts) from relational, set-oriented data structures, which are common in database systems, to the object-oriented patterns predominantly used in application development today.

While certainly useful in that they allow you to use a reliable and scalable relational database as data storage for your application these frameworks face an unavoidable impedance mismatch. They often provide leaky abstractions at best by papering over the fundamental mathematical differences between sets (relational) and graphs (object-oriented). This impedance mismatch will always be there. You can gloss over it to some extent but it’ll crop up every now and then.

Therefore it’s a good idea we don’t fight this discrepancy but embrace it and keep the systems dealing with objects and sets as simple as possible by not trying to force object-orientation on relations and vice versa.

This is where JdbcTemplate comes in handy. Instead of defining rigid database mappings it allows you to have ordinary, native and flexible SQL queries like this in your code:


...

List<SomeEntity> entities =
        jdbcTemplate.query(
            "SELECT a.title, b.text FROM a JOIN b ON b.id = a.id",
            new SomeEntityMapper()
        );

...

This query uses a mapper class that implements org.springframework.jdbc.core.RowMapper and for example could like look this:


public class SomeEntitykMapper implements RowMapper<SomeEntity> {
    public SomeEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
        SomeEntity someEntity = new SomeEntity();

        someEntity.setId(rs.getInt("id"));
        someEntity.setTitle(rs.getString("title"));
        someEntity.setText(rs.getString("text"));
        
        return someEntity;
    }
}

This technique allows for flexible mapping between relational data and an object-oriented representation. If the class’ field names and the result set’s column names match you can even use a BeanRowPropertyMapper and do without the custom mapper altogether.

Leave a Comment

By continuing to use the site you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or if you click "Accept" below then you are consenting to this.

Close