Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA vs Spring JdbcTemplate [closed]

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?
like image 661
aem999 Avatar asked Jan 01 '11 11:01

aem999


People also ask

Which is better JDBCTemplate or JPA?

At work we use Hibernate JDBCTemplate because it has more flexibility. It also has better performance than JPA because you are not "loading" a lot of unnecessary data into your app. In the JDBCTemplate case, your SQL skills go a long way in giving you exactly what you need at the right speed.

Will Spring JDBCTemplate close connection?

Spring JdbcTemplates will just grab connections from your data source and pool them appropriately based on the data source. You don't have to worry about if they are opened, closed, or whatever.

Does Spring JPA close connection?

The application closes the connection, which returns the connection to the pool. Note: The application calls the close() method, which allows the connection to remain open. The pool receives the notification of the close request.


1 Answers

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.

like image 150
Timo Westkämper Avatar answered Sep 21 '22 21:09

Timo Westkämper