什么是 JPA 实现? [英] What is a JPA implementation?

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

问题描述

我正在开始使用 JPA,但我对 JPA 实现(EclipseLink、TopLink、Hibernate 等)究竟做了什么感到困惑.

I'm getting started with JPA, and I'm confused as to what exactly the JPA implementation(EclipseLink, TopLink, Hibernate, etc.) does.

我了解JPA的理论作用,但是各种实现的目的是什么?这些选择之间是否存在显着差异,正如所说的 DB 连接器/驱动程序?哪个最适合初学者?

I understand the theoretical role of JPA, but what is the purpose of the various implementations? Are there significant differences between these choices, as there is with say, DB connectors/drivers? Which is the best one for a beginner?

我可能会选择 EclipseLink,因为我读过的大部分文献都使用它.

I'll probably go with EclipseLink because that is what most of the literature I've read uses.

推荐答案

JPA 只是一个需要实现才能使用的 API(因此是 Java Persistence API).

JPA is just an API (hence Java Persistence API) that requires an implementation to use.

类比是使用 JDBC.JDBC 是用于访问数据库的 API,但您需要一个实现(驱动程序 jar 文件)才能连接到数据库.就其本身而言,没有驱动程序,您无法对数据库进行任何操作.

An analogy would be using JDBC. JDBC is an API for accessing databases, but you need an implementation (a driver jar file) to be able to connect to a database. On its own, without a driver, you cannot do anything with a database.

正如我所说,使用 JPA,您需要一个实现,一组位于 JPA下方"的类,这样的实现将满足您的需求.

With JPA, as I said, you need an implementation, a set of classes that lie "below" JPA, and such an implementation will do what you want.

您的应用程序使用 JPA API(这个措辞有点笨拙,但我希望您能理解),然后与底层实现进行通信.

Your application uses the JPA API (this wording is a bit cubersome, but I hope you get the idea), which then communicates with the underlying implementation.

流行的实现包括 HibernateEclipseLinkOpenJPA 等.

Popular implementations include Hibernate, EclipseLink, OpenJPA and others.

它们中的每一个都实现了 JPA API,因此如果您只使用 JPA,那么每个实现的行为都应该相同.

Every one of them implement the JPA API, so if you use only JPA, every implementation should act the same.

但是!这些实现提供的功能可能超出标准 JPA API.

But! The functionality provided by these implementations might go beyond the standard JPA API.

如果您想使用此特定功能,则必须使用与其他供应商不兼容的特定于供应商的 API.

If you want to use this particular functionality, you will have to use vendor specific API that will not be compatible with others.

例如,即使 JPA 定义了带有 ID 生成选项的 @Id 注释,但在使用 Hibernate 时,您也可以使用 @org.hibernate.annotations.GenericGeneratorHibernate 特定的生成策略.

For example, even though JPA defines the @Id annotation with ID generation options, when using Hibernate, you can use also @org.hibernate.annotations.GenericGenerator for Hibernate specific generation strategies.

除非您使用 Hibernate 作为底层实现,否则无法使用此注释.

Using this annotation will not work unless you're using Hibernate as the underlying implementation.

底线是:JPA 是每个供应商都实现的最小公分母",每个实现都可能具有一些非标准的高级功能.

The bottom line is: JPA is the "least common denominator" which every vendor implements, and every implementation might have some more advanced features that aren't standard.

如果您希望应用程序可移植,请仅使用 JPA.如果您确定以后不会改变主意并切换实现,请使用 JPA + 供应商特定功能.

If you want your application to be portable, use only JPA. If you are sure you won't change your mind later on and switch implementations, use JPA + vendor specific features.

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

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