aem999 aem999 - 8 months ago 81
Java Question

JPA vs Spring JdbcTemplate

For a new project is JPA always the recommended tool for handling relational data or are there scenarios where Spring JdbcTemplate is a better choice? Some factors to consider in your response:

  • new database schema vs pre-existing schema and tables

  • level of developer expertise

  • ease with which can integrate with a data caching layer

  • performance

  • any other relevant factors to consider?

Answer Source

Use Spring JdbcTemplate if you don't want to access your database schema via a domain model. Using JdbcTemplate you are using a lower level access, with more flexibility, but probably also more boilerplate.

Spring JdbcTemplate can be more easily used with exotic database schemas and a stored procedure focus. Using JPA you need to make sure that database schema maps correctly to the domain model.

Both technologies need developers knowing relational databases, SQL and transactions. With JPA you get more hidden complexity though.

JPA is to my knowledge more easily pluggable to data caching layers, since the object oriented focus makes cache entry identification, update and invalidation easier.

You can fine tune JdbcTemplate based backends better, but there is for most cases more code involved.

Some other aspect to consider is that although with JPA you get a domain model for your database schema you will often need to use additional DTO classes. Using JdbcTemplate you can directly operate with DTO classes.