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:

  • 如果您加载一个实体,则会运行 SQL 语句。完成之后,您将拥有一个完全已加载的实体。不会执行延迟加载或缓存。

  • 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.

  • 有一个简单的模型,用于将实体映射到表。它可能仅适用于相当简单的情况。如果您不喜欢这种情况,则应编写您自己的策略。Spring 数据 JDBC 仅提供非常有限的支持,用于使用注释自定义该策略。

  • 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.