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:

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

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.

About the author: Bjoern
Independent IT consultant, entrepreneur


  • 💬 Rarely Used But Useful PostgreSQL Features | Björn Wilmsmann

Leave a Comment