Why Spring Data JDBC?

在 Java 世界中,关系型数据库的主要持久性 API 当然是 JPA,它拥有自己的 Spring Data 模块。为什么还有另一个?

The main persistence API for relational databases in the Java world is certainly JPA, which has its own Spring Data module. Why is there another one?

JPA 会做许多事情来帮助开发者。除其他事项外,它跟踪对实体的更改。它会做一些延迟加载,它让你能够将广泛的对象构造映射到同样广泛的数据库设计。

JPA does a lot of things in order to help the developer. Among other things, it tracks changes to entities. It does lazy loading for you. It lets you map a wide array of object constructs to an equally wide array of database designs.

这非常不错,并且让许多事情变得非常容易。只需看一个基本的 JPA 教程。但是,JPA 为什么会做某些事情往往让人感到非常困惑。此外,那些概念上非常简单的事物使用 JPA 会变得相当困难。

This is great and makes a lot of things really easy. Just take a look at a basic JPA tutorial. But it often gets really confusing as to why JPA does a certain thing. Also, things that are really simple conceptually get rather difficult with JPA.

Spring Data JDBC 的目标是在概念上变得简单的多,方法是采用下列设计决策:

Spring Data JDBC aims to be much simpler conceptually, by embracing the following design decisions:

  • If you load an entity, SQL statements get run. Once this is done, you have a completely loaded entity. No lazy loading or caching is done.

  • If you save an entity, it gets saved. If you do not, it does not. There is no dirty tracking and no session.

  • There is a simple model of how to map entities to tables. It probably only works for rather simple cases. If you do not like that, you should code your own strategy. Spring Data JDBC offers only very limited support for customizing the strategy with annotations.