Home > Back-end >  Spring initialzr - At SQL category, what do I choose between JDBC API, Spring Data JPA and Spring Da
Spring initialzr - At SQL category, what do I choose between JDBC API, Spring Data JPA and Spring Da

Time:01-17

I'm doing a learning project and I want to create an API. I will be using PostgreSQL. I'm setting up my project with Spring Initializr and I'm a bit confused regarding the SQL options.

Which should I choose? I tried to research the differences between each other and I am inclined to use Spring Data JPA. Can someone with experience shine some light, in layman's terms, about the differences and suitability of these 3 options?

screen capture from spring initializr depicting the 3 main SQL options

CodePudding user response:

JDBC

itself is the low level API to interact with SQL databases.

It is a pain to use, since you have to create each and every SQL statement on your own using String values, handle all the checked exceptions in often non-trivial ways and map results to your domain classes.

All this is tedious and error prone, so you almost certainly want something on top of it.

JDBC API

in Spring Initializr is a bunch of classes that are part of Spring Framework, that help with the repetitiveness and error proneness of the tasks mentioned above but doesn very little beyond that. If you use JDBC API you will mainly work with JdbcTemplate or NamedParameterJdbcTemplate.

Most of error handling will be done for you. Mapping to domain object is is done by implementing RowMapper or ResultSetExtractor interfaces, though you might choose DataClassRowMapper for easy cases. The SQL you still have to create by hand.

Spring Data

Is an set of Spring modules that abstract over database access. It offers similar API for very different data stores. Think MongoDb, Neo4J, Redis, ElasticSearch, ... and of course SQL databases. For relational databases it offers the two variants: Spring Data JPA and Spring Data JDBC.

It revolves around the concept of Repository which comes from Domain Driven Design (DDD). The idea of a repository is that it is similar to a collection that happens to persist its data. Ideally the interface should be agnostic to the persistence technology used.

Spring Data will create such repositories for you, without much work on your side. This means for basic tasks you don't have to create any SQL at all.

Spring Data JPA

is based on JPA, which stands for Jakarta/Java Persistence API, which in turn builds on JDBC. It is the most used Spring Data module and is extremely widely used in the industry. While JPA seems easy to use in the beginning, it certainly isn't simple to really understand. I did talk which should get you started with Spring Data JPA while also pointing out it most important pitfalls. One of the problems is that the main idea of JPA kind of contradicts the main ideas of DDD.

Spring Data JDBC

Is build directly build on Springs JDBC API mentioned above. It is conceptually much simpler than JPA and really honours the ideas of DDD. That said it has way less features (partly to make it simpler, partly because it is a much smaller project) than JPA Spring Data JPA.

I recommend reading Spring Data JDBC, References, and Aggregates and maybe watch Domain-Driven Design with Relational Databases Using Spring Data JDBC

So which should you pick?

This is of course what you want to achieve. Since this is a learning project for you I see the following potential arguments for each of the three options:

You want to learn how relational databases work from the ground up.

Pick JDBC API. It is a little like opening the hood of your car an tinkering with what you find. It is dirty, but it is the only way to learn what is really going on.

Note that you'll get the JDBC API anyway even when you use one of the other two options. But playing with two or more approaches at the same time might be a little to much for the start.

You want to learn the basics that look best in your CV for the next junior job

Pick Spring Data JPA. It is the industry standard. Chances that your next Java job will use it are rather high. You'll find tons of resources (including tons that are wrong;-) And the inherent complexities of JPA probably won't hurt you much in a little toy project. And if they do you learned something valuable for the next job interview.

You want to learn about DDD and how it helps to structure your application

Pick Spring Data JDBC. It is conceptually cleaner and it will help you to write your application in a modular fashion. And you'll better understand how Spring Data is intended to be used. This will be helpful, even when you later are forced to use Spring Data JPA by your employer.

  • Related