Hibernate和Spring Data JPA的区别是什么

时间:2021-07-31 20:08:37

What are the main differences between Hibernate and Spring Data JPA? When should we not use Hibernate or Spring Data JPA? Also, when may Spring JDBC template perform better than Hibernate / Spring Data JPA?

Hibernate和Spring Data JPA的主要区别是什么?我们什么时候不应该使用Hibernate或Spring Data JPA?此外,什么时候Spring JDBC模板的性能会比Hibernate / Spring数据JPA更好?

2 个解决方案

#1


128  

Hibernate is a JPA implementation, while Spring Data JPA is a JPA Data Access Abstraction. Spring Data offers a solution to GenericDao custom implementations. It can also generate JPA queries on your behalf through method name conventions.

Hibernate是一个JPA实现,而Spring Data JPA是一个JPA数据访问抽象。Spring Data为GenericDao定制实现提供了一个解决方案。它还可以通过方法名称约定代表您生成JPA查询。

With Spring Data, you may use Hibernate, Eclipse Link or any other JPA provider. A very interesting benefit is that you can control transaction boundaries declaratively using the @Transactional annotation.

使用Spring数据,您可以使用Hibernate、Eclipse链接或任何其他JPA提供者。一个非常有趣的好处是,您可以使用@Transactional注释以声明的方式控制事务边界。

Spring JDBC is much more lightweight, and it's intended for native querying, and if you only intend to use JDBC alone, then you are better off using Spring JDBC to deal with the JDBC verbosity.

Spring JDBC要轻得多,并且它是用于本机查询的,如果您只想单独使用JDBC,那么最好使用Spring JDBC来处理JDBC冗长。

So, Hibernate,and Spring Data are complementary rather than competitors.

因此,Hibernate和Spring数据是互补的,而不是竞争对手。

#2


46  

There are 3 different things we are using here :

我们这里有三种不同的用法:

  1. JPA : Java persistence api which provide specification for persisting, reading, managing data from your java object to relations in database.
  2. JPA: Java persistence api,它提供了持久化、读取、管理Java对象数据到数据库关系的规范。
  3. Hibernate: There are various provider which implement jpa. Hibernate is one of them. So we have other provider as well. But if using jpa with spring it allows you to switch to different providers in future.
  4. Hibernate:有各种实现jpa的提供者。Hibernate就是其中之一。我们还有其他的供应商。但是如果在spring中使用jpa,您将来可以切换到不同的提供者。
  5. Spring Data JPA : This is another layer on top of jpa which spring provide to make your life easy.
  6. Spring Data JPA:这是JPA之上的另一层,Spring为您的生活提供了方便。

So lets understand how spring data jpa and spring + hibernate works-

因此,让我们了解spring data jpa和spring + hibernate是如何工作的-


Spring Data JPA:

Let's say you are using spring + hibernate for your application. Now you need to have dao interface and implementation where you will be writing crud operation using SessionFactory of hibernate. Let say you are writing dao class for Employee class, tomorrow in your application you might need to write similiar crud operation for any other entity. So there is lot of boilerplate code we can see here.

假设您在应用程序中使用spring + hibernate。现在您需要有dao接口和实现,您将在其中使用hibernate的SessionFactory编写crud操作。假设您正在为Employee类编写dao类,明天在您的应用程序中,您可能需要为任何其他实体编写相似的crud操作。这里有很多样板代码。

Now Spring data jpa allow us to define dao interfaces by extending its repositories(crudrepository, jparepository) so it provide you dao implementation at runtime. You don't need to write dao implementation anymore.Thats how spring data jpa makes your life easy.

现在,Spring data jpa允许我们通过扩展其存储库(crudrepository, jparepository)来定义dao接口,以便在运行时为您提供dao实现。您不再需要编写dao实现。这就是spring数据jpa使您的生活变得轻松的原因。

#1


128  

Hibernate is a JPA implementation, while Spring Data JPA is a JPA Data Access Abstraction. Spring Data offers a solution to GenericDao custom implementations. It can also generate JPA queries on your behalf through method name conventions.

Hibernate是一个JPA实现,而Spring Data JPA是一个JPA数据访问抽象。Spring Data为GenericDao定制实现提供了一个解决方案。它还可以通过方法名称约定代表您生成JPA查询。

With Spring Data, you may use Hibernate, Eclipse Link or any other JPA provider. A very interesting benefit is that you can control transaction boundaries declaratively using the @Transactional annotation.

使用Spring数据,您可以使用Hibernate、Eclipse链接或任何其他JPA提供者。一个非常有趣的好处是,您可以使用@Transactional注释以声明的方式控制事务边界。

Spring JDBC is much more lightweight, and it's intended for native querying, and if you only intend to use JDBC alone, then you are better off using Spring JDBC to deal with the JDBC verbosity.

Spring JDBC要轻得多,并且它是用于本机查询的,如果您只想单独使用JDBC,那么最好使用Spring JDBC来处理JDBC冗长。

So, Hibernate,and Spring Data are complementary rather than competitors.

因此,Hibernate和Spring数据是互补的,而不是竞争对手。

#2


46  

There are 3 different things we are using here :

我们这里有三种不同的用法:

  1. JPA : Java persistence api which provide specification for persisting, reading, managing data from your java object to relations in database.
  2. JPA: Java persistence api,它提供了持久化、读取、管理Java对象数据到数据库关系的规范。
  3. Hibernate: There are various provider which implement jpa. Hibernate is one of them. So we have other provider as well. But if using jpa with spring it allows you to switch to different providers in future.
  4. Hibernate:有各种实现jpa的提供者。Hibernate就是其中之一。我们还有其他的供应商。但是如果在spring中使用jpa,您将来可以切换到不同的提供者。
  5. Spring Data JPA : This is another layer on top of jpa which spring provide to make your life easy.
  6. Spring Data JPA:这是JPA之上的另一层,Spring为您的生活提供了方便。

So lets understand how spring data jpa and spring + hibernate works-

因此,让我们了解spring data jpa和spring + hibernate是如何工作的-


Spring Data JPA:

Let's say you are using spring + hibernate for your application. Now you need to have dao interface and implementation where you will be writing crud operation using SessionFactory of hibernate. Let say you are writing dao class for Employee class, tomorrow in your application you might need to write similiar crud operation for any other entity. So there is lot of boilerplate code we can see here.

假设您在应用程序中使用spring + hibernate。现在您需要有dao接口和实现,您将在其中使用hibernate的SessionFactory编写crud操作。假设您正在为Employee类编写dao类,明天在您的应用程序中,您可能需要为任何其他实体编写相似的crud操作。这里有很多样板代码。

Now Spring data jpa allow us to define dao interfaces by extending its repositories(crudrepository, jparepository) so it provide you dao implementation at runtime. You don't need to write dao implementation anymore.Thats how spring data jpa makes your life easy.

现在,Spring data jpa允许我们通过扩展其存储库(crudrepository, jparepository)来定义dao接口,以便在运行时为您提供dao实现。您不再需要编写dao实现。这就是spring数据jpa使您的生活变得轻松的原因。