Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 770
PaulEdison Avatar asked Aug 20 '18 01:08

PaulEdison


People also ask

What is the difference between Spring data JDBC and Spring data JPA?

JDBC is database-dependent, which means that different scripts must be written for different databases. On the other side, JPA is database-agnostic, meaning that the same code can be used in a variety of databases with few (or no) modifications.

What is the difference between JDBC and Spring JDBC?

The Spring JDBC Template has the following advantages compared with standard JDBC. The Spring JDBC template allows to clean-up the resources automatically, e.g. release the database connections. The Spring JDBC template converts the standard JDBC SQLExceptions into RuntimeExceptions.

What is Spring data JDBC?

Spring Data JDBC is an object-relational mapping framework for relational databases that aims to avoid most of the complexity of other ORM frameworks. It does that by avoiding features like lazy loading, managed lifecycles of entity objects and caching.

Should I use Spring data JPA or JDBC?

Spring Data JDBC has less abstractions than Spring Data JPA, but uses Spring Data concepts to make it easier to do CRUD operations than Spring JDBC. It sits closer to the database because it does not contain the most part of the Spring Data magic when querying the database.


1 Answers

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
}
like image 90
Dovmo Avatar answered Oct 31 '22 02:10

Dovmo