spring-jdbc vs spring-data-jdbc and what are they supporting

PaulEdison picture PaulEdison · Aug 20, 2018 · Viewed 8.9k times · Source

I'm curious what is the difference between the spring-jdbc (what I missing in the newest spring release) and spring-data-jdbc.
Is there a difference or just a renaming (in the repositories I don't see this)?

And is there somewhere described what are the supported targets(DB/JDBC specs/JDK) of the versions?

e.g. for the plain JDBC from oracle I can see that information here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#01_03_1
(e.g.: JDBC Spec 4.1 in ojdbc7.jar on Java7/Java8 on Oracle DB 12.1/12cR1)

But I miss that for spring-jdbc - where do I find that information?

Answer

Dovmo picture Dovmo · Aug 20, 2018

spring-jdbc

The docs for spring-jdbc are basically here:

https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html

Though it doesn't specifically point you to the Spring project spring-jdbc. This project just provides all of the Spring abstractions over the plain JDBC DataSource that you can use with the Spring Framework. For example, Spring's DataSources which nicely hook into Spring's Transaction management capabilities, like the @Transactional annotation. Also, the JdbcTemplate is part of this module, which allows you to execute SQL statements and extract objects from ResultSets without dealing with exception handling or the nasty details of properly closing statements, connections and the like.

spring-data-jdbc

spring-data-jdbc, on the other hand, provides the Spring Data abstraction over spring-jdbc. That is, you can create a Spring Data CrudRepository and a simple "entity" (not a JPA entity!) and, as Spring Data does, it will create your queries for you without you having to write native CRUD queries over JDBC, as in this example on the spring-data-examples git repo.

Using the referenced example as a demonstration:

interface CategoryRepository extends CrudRepository<Category, Long> {}

The above code is all you could need (using introspection on the Category object name as the source for the table name (based on a NamingStrategy) and it's properties as columns, again similar to JPA, but not using JPA.

Rather than writing your own like so:

@Repository
public class CategoryRepository {
   public void create(Category category) {
      jdbcTemplate.execute("insert...");
   }

  // The rest of my other CRUD operations
}