spring-jdbc vs spring-data-jdbc and what are they supporting
Asked Answered
C

1

29

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?

Cismontane answered 20/8, 2018 at 1:11 Comment(0)
P
28

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
}
Punctual answered 20/8, 2018 at 1:29 Comment(13)
Perhaps you can illustrate this with an example? Maybe a quick implementation of the CrudRepo vs the traditional approach.Battles
Edited. That should explain what the example is doing a little bit betterPunctual
Good explanation - but still I'm missing what version of the frameworks are supporting what JDBC Specification. I flew over the document but couldn't find anything about this.Cismontane
docs.spring.io/spring/docs/current/spring-framework-reference/… In that first paragraph, it says All approaches require a JDBC 2.0-compliant driver, and some advanced features require a JDBC 3.0 driver.Punctual
To understand that - spring-jdbc requires a real JDBC driver ... ? But I got a project using spring-jdbc but cant find any JDBC driver dependency on it (using mvn dependency:tree -Dverbose) as I would expect after reading this paragraph form the documentCismontane
spring-jdbc is Spring's convenient abstraction over JDBC. The idea is that you can use whatever database and whatever JDBC-compliant driver you want for that database, wrap that resource in a Spring DataSource and let Spring take care of managing Transactions for you, etc.Punctual
@Dovmo: But how can it abstract it if it is not part of the final dependency tree what does it abstract if there is nothing.Cismontane
@Cismontane JDBC is part of the Java SE, so there is no explicit dependency necessary for that. You have to add the JDBC driver implementation that you actually want to use.Telemetry
@JensSchauder: But if I didn't what is it using as I said there is none ...Cismontane
@Cismontane somewhere there is a Jdbc Driver. It won't work without. Maybe you are using Java DB without noticing? netbeans.org/kb/docs/ide/java-db.htmlTelemetry
@JensSchauder: it should be somewhere - but I don't see any ... how could I determine where it is hidden as using mvn dependency:tree -Dverbose seems not working?Cismontane
@Cismontane if you run your application with java -verbose:class instead of just java it will list all the classes loaded, including the location it got loaded from. If you grep that for "jdbc" or "driver" you should be able to find it.Telemetry
@Cismontane Specific DB driver must be somewhere on the class path: 1. in jar (defined e.g. in maven) 2. in the application server (jars folder) 3. manually added (registered/added to classpath, etc.). If you try to connect your app to different DB it will fail (e.g. Oracle vs. Haidi vs. H2 drivers). You can setup new clean project from scratch and try to work just with java JDBC - you will be needed add specific DB driver to class path. Since you will need write a lot of boilerplate code, you have two options: either write utils yourself or you can use Spring JDBC.Martres

© 2022 - 2024 — McMap. All rights reserved.