JPA 和 Spring Data JPA 有什么区别? [英] What's the difference between JPA and Spring Data JPA?

查看:19
本文介绍了JPA 和 Spring Data JPA 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Spring Data-JPA 和 JPA 之间的区别有点困惑.我知道 JPA 是一种使用流行的 ORM 技术将 Java 对象持久化到关系数据库的规范.

I am a bit confused about the difference between Spring Data-JPA and JPA. I know about JPA that it is a specification for persisting the Java Objects to a relational database using popular ORM technology.

换句话说,JPA 提供接口和其他 ORM 技术,实现那些称为 JPA 提供程序的接口,例如 Hibernate.

In other words, JPA provides interfaces and other ORM technologies, implements those interfaces known as JPA provider e.g Hibernate.

现在,Spring Data JPA 到底是什么?

Now, what exactly is Spring Data JPA?

Spring Data JPA 是在 JPA 之上添加了一些更多的功能(接口)并且仍然只是指定的还是它也是一个 JPA 提供者?

Is Spring Data JPA has added some more functionality (Interfaces) over JPA and still it is specified only or it is also a JPA provider?

我看到 Spring Data JPA 围绕存储库工作(DAO 层:如果我没记错的话).所以,我的意思是,使用Spring Data JPA + Hibernate"或仅使用Hibernate"定向有何不同?

I saw Spring Data JPA works around repositories (DAO layer: if I am not wrong). So, I mean, how it is different using 'Spring Data JPA + Hibernate' or only using 'Hibernate' directing?

推荐答案

我看到了 Spring,JPA 围绕存储库工作(DAO 层:如果我没记错的话).所以我的意思是使用Spring JPA + Hibernate"或仅直接使用Hibernate"有何不同?

I saw Spring, JPA works around repositories (DAO layer: if I am not wrong). So I mean how it is different using 'Spring JPA + Hibernate' or only using 'Hibernate' directly?

如您所说,JPA 是一种规范,而 Hibernate 是该规范的特定实现(这些实现通常称为 提供者).通过使用 Hibernate,您将自己与该提供者联系在一起,从而限制了您在需要时切换到另一个选项的自由(例如,您想改用 EclipseLink 或 ObjectDB,因为 Hibernate 有一个错误会停止您的开发过程).

As you said, JPA is an specification while Hibernate is a particular implementation of that specification (these implementations are usually referred to as Providers). By using Hibernate you tie yourself to that provider restricting your freedom to switch to another option when required (for example, you want to use EclipseLink or ObjectDB instead because Hibernate has a bug that halts your development process).

引用 Spring Data JPA 文档:

Quoting Spring Data JPA's documentation:

实现应用程序的数据访问层已经很麻烦了.必须编写太多样板代码.领域类缺乏活力,并没有以真正的面向对象或领域驱动的方式设计.

Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code had to be written. Domain classes were anemic and haven't been designed in a real object oriented or domain driven manner.

使用这两种技术可以让开发人员在富域模型的持久性方面更轻松.尽管如此,用于实现存储库的样板代码量仍然相当高.因此,Spring Data 存储库抽象的目标是显着减少为各种持久性存储实现数据访问层的工作量.

Using both of these technologies makes developers life a lot easier regarding rich domain model's persistence. Nevertheless the amount of boilerplate code to implement repositories, especially is still quite high. So the goal of the repository abstraction of Spring Data is to reduce the effort to implement data access layers for various persistence stores significantly.

总而言之,它在 JPA 之上添加了另一个抽象层,类似于定义基于标准的设计以在 Spring 上下文中支持持久层.那些定义的接口(Spring 已知)提供框架使用 JPA 处理的服务来提供结果.您以 Spring 可以扫描项目并找到它的方式定义存储库:

To sum it up, it is on top of JPA adding another layer of abstraction, kind of defining a standard-based design to support Persistence Layer in a Spring context. Those defined interfaces (known to Spring) provide the services that the framework handles using JPA to serve the results. You define a repository in a way Spring can scan the project and find it:

<repositories base-package="com.acme.repositories" />

因此,允许您在容器的上下文中或在容器外部使用它.

Thus, allowing you to use it in the context of a container or outside of it.

Spring 到底是什么,JPA.是 Spring,JPA 在 JPA 之上添加了更多功能(接口),但它仍然只是指定的还是 JPA 提供者?

Now what exactly is Spring, JPA. Is Spring, JPA has added some more functionality (Interfaces) over JPA and still it is specified only or it is also a JPA provider?

Spring Data JPA 提供了一个定义,通过引用 JPA 规范,使用您定义的提供程序来实现底层支持的存储库.

Spring Data JPA provides a definition to implement repositories that are supported under the hood by referencing the JPA specification, using the provider you define.

这篇关于JPA 和 Spring Data JPA 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆