简体   繁体   English

我们可以在没有休眠的情况下使用 jpa

[英]can we use jpa without hibernate

I am new to JPA.我是 JPA 的新手。

As per my understanding, JPA is specification and Hibernate implements JPA and provide add on features along with JPA methods.根据我的理解,JPA 是规范,Hibernate 实现 JPA 并提供附加功能以及 JPA 方法。

But I was going through JPA tutorials, where using EntityManager object we can perform CRUD on data, without using Hibernate libraries!但是我正在阅读 JPA 教程,其中使用EntityManager对象我们可以对数据执行 CRUD,而无需使用 Hibernate 库!

So can anyone please let me know所以任何人都可以请让我知道

  1. Can I use JPA alone using EntityManager methods, not using Hibernate?我可以使用EntityManager方法单独使用 JPA,而不是使用 Hibernate 吗?

Please mention some useful link or example.请提及一些有用的链接或示例。

Much appreciate response.非常感谢回应。
Thanks.谢谢。

You are right JPA is a specification.你说得对 JPA 是一个规范。 Hibernate, and EclipseLink are a couple of its implementations. Hibernate 和 EclipseLink 是它的几个实现。

You have to specify the persistence provider(Hibernate, EclipseLink) in order to use the JPA implementation.您必须指定持久性提供程序(Hibernate、EclipseLink)才能使用 JPA 实现。 The persistence providers have the implementation classes for JPA specifications.持久性提供程序具有 JPA 规范的实现类。

You can't just use JPA, cause it is an API =), but there are plenty JPA implementations:你不能只使用 JPA,因为它是一个 API =),但是有很多 JPA 实现:

  1. EclipseLink Eclipse链接
  2. ObjectDB对象数据库

If you don't want to use Hibernate (or any other JPA provider), then you must implement your own provider, by giving an implementation for the javax.persistence.spi.PersistenceProvider interface.如果您不想使用 Hibernate(或任何其他 JPA 提供程序),那么您必须通过提供 javax.persistence.spi.PersistenceProvider 接口的实现来实现您自己的提供程序。 The question, is, why do you need re-invent the wheel ?问题是,你为什么需要重新发明轮子? If you don't like Hibernate, you can use one of the so many other JPA-Providers to choose from如果您不喜欢 Hibernate,您可以使用众多其他 JPA 提供程序之一进行选择

JPA can be used without a JPA provider aka Hibernate, EclipseLink and so on only if the application server has already a JPA implementation.仅当应用程序服务器已经有 JPA 实现时,JPA 才可以在没有 JPA 提供程序(又名 Hibernate、EclipseLink 等)的情况下使用。 Most likely on the tutorials you have seen demos that were perfomed on such an application server.您很可能在教程中看到过在此类应用程序服务器上执行的演示。

Though this thread is pretty old, I felt it is worth breaking things down.虽然这个线程已经很老了,但我觉得值得分解一下。

JPA: It Is just a specification. JPA:这只是一个规范。 In simpler words.用更简单的话来说。 Set of interfaces.接口集。

Hibernate, Eclipse Link: Two of the many implementations of JPA. Hibernate、Eclipse Link: JPA 的众多实现中的两个。 In addition to providing the basic implementations for the JPA specifications, Hibernate and Eclipse Link provide their additional functionalities.除了为 JPA 规范提供基本实现之外,Hibernate 和 Eclipse Link 还提供了它们的附加功能。 You can choose based on your need您可以根据自己的需要选择

Spring Data:春季数据:

  • It provides additional abstraction.它提供了额外的抽象。
  • When you use Hibernate/Eclipse Link you still have to write some boilerplate code.当您使用 Hibernate/Eclipse Link 时,您仍然需要编写一些样板代码。 By using Spring JPA you can avoid that.通过使用 Spring JPA,您可以避免这种情况。
  • The most important thing to note is Spring data uses Hibernate by Default due to Springboot's Opinionated nature.需要注意的最重要的一点是 Spring 数据由于 Springboot 的 Opinionated 性质而默认使用 Hibernate。 You can change the default behavior if you would like.如果您愿意,可以更改默认行为。
  • When you run the following command in a Springboot project that uses Spring JPA(with default configuration), you will see Hibernate jars being used.当您在使用 Spring JPA(具有默认配置)的 Springboot 项目中运行以下命令时,您将看到正在使用 Hibernate jar。

Maven: mvn dependency:tree Maven: mvn dependency:tree

Gradle: gradle dependencies Gradle: gradle dependencies

You can use JPA alone without using Hibernate and Before should know about major points in between Hibernate vs JPA as given below.您可以单独使用 JPA 而不使用 Hibernate,并且 Before 应该了解 Hibernate 与 JPA 之间的要点,如下所示。

Hibernate休眠

  1. Hibernate is a ORM Framework which is support complete ORM and also use JPA features. Hibernate 是一个 ORM 框架,它支持完整的 ORM 并使用 JPA 功能。
  2. Second level Cache is available so Performance is very good二级缓存可用,性能非常好
  3. Its support .Net using NHibernate tool它使用 NHibernate 工具支持 .Net
  4. Its generate HQL(Hibernate Query Language)它生成 HQL(Hibernate Query Language)

JPA日本特许经营协会

  1. JPA is part of EJB specification which is released in J2EE 1.5 and this will use for java as well as J2EE JPA 是在 J2EE 1.5 中发布的 EJB 规范的一部分,这将用于 Java 和 J2EE
  2. Second Level Cache is not available so Performance is not good.二级缓存不可用,因此性能不佳。
  3. Its not support to .Net它不支持 .Net
  4. Its generate JPQL(Java Persistence Query Language)它生成 JPQL(Java Persistence Query Language)
  5. Top of any persistence provider like Hibernate we can use JPA.在任何像 Hibernate 这样的持久化提供程序之上,我们可以使用 JPA。

you can't use JPA on its own.您不能单独使用 JPA。 JPA is a specification, which defines an API for object-relational mappings and for managing persistent objects and you need a JPA provider to implement it, like Hibernate, EclipseLink. JPA 是一种规范,它定义了用于对象-关系映射和管理持久对象的 API,您需要一个 JPA 提供程序来实现它,例如 Hibernate、EclipseLink。

No, you cannot perform CRUD operations with JPA alone.不,您不能单独使用 JPA 执行 CRUD 操作。 As JPA is just a specification, you need the implementation to perform database operations.由于 JPA 只是一个规范,您需要实现来执行数据库操作。 The implementations are provided by Hibernate, EclipseLink, Ibatis, etc.实现由 Hibernate、EclipseLink、Ibatis 等提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM