3

My question just simple : what are the main differences between Spring jdbcTemplate and Hibernate ? what are the main reasons we should take into account for using one or the other ?

Thanks

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
kpedia
  • 207
  • 1
  • 4
  • 9
  • Spring JDBC Template is not a JPA/ORM implementation, hibernate is. Perhaps spring-orm to hibernate is a more appropriate comparison. – CoolBeans Jun 25 '13 at 15:18

2 Answers2

16

Hibernate is a really huge solution with data persistence and ORM including JPA implementation. Also, there are defined many ways how to manage entities in Hibernate, how to persist, transactions, etc. In hibernate you can use SQL, HQL or java annotations. JDBC template is just a simple tool that helps you to manage SQL queries and transactions. It is probably better described as a JDBC wrapper or helper. If you prefer managing database queries (SQL) yourself or if you are a beginner, using Spring JdbcTemplate will help you understand how it works. Even if you are working on a bigger application, think about using Hibernate. Just be wary of the learning curve of Hibernate.

brhardwick
  • 3,065
  • 2
  • 15
  • 17
Martin Strejc
  • 4,307
  • 2
  • 23
  • 38
  • thanks for your reply. So can I use Spring JdbcTemplate in a project which don't use any other spring modules ? – kpedia Jun 26 '13 at 07:33
  • Yes. Just be careful for transactions. I'm not sure if transaction manager works correctly without proxies/beans. – Martin Strejc Jun 26 '13 at 11:10
  • 1
    when I'm working with millions of records per day I prefer a simple and clean tool like spring-jdbc, it controls connections and transactions for me and give me all native SQL power to perform the best query for each case – deFreitas Apr 21 '18 at 01:18
  • Btw, you can use native SQL queries in Hibernate as well, but you will lose DB engine independence. – Martin Strejc Apr 21 '18 at 09:00
11

It all depends. If you doing a lot of mapping of domain objects to database tables, Hibernate is the solution you'd want to look at. If you are writing a reporting tool that does lots of complicated joins / PL-SQL, look into Spring JDBC Templates.

aglassman
  • 2,643
  • 1
  • 17
  • 30
  • is there any reason to choose JDBCtemplate over plain jdbc other than the reason of avoiding boilerplate code and auto close connection? – Stunner Feb 26 '20 at 10:28
  • my scenario is I have some 100 queries.. irrespective of plain jdbc or jdbctemplate , I have to pass the queries or hardcode or put it as constants in inerface... but code is looking clumpsy.. i thought i better use plain jdbc so i have control – Stunner Feb 26 '20 at 10:30
  • Anything you can do in plain JDBC, you can do in JdbcTemplate, but much easier. I'd highly suggest using JdbcTemplate, or NamedParameterJdbcTemplate whenever possible. I'd suggest reading up on JdbcTemplate a bit more if you're not sold on it yet. – aglassman Feb 28 '20 at 14:31