简体   繁体   English

Java Spring hibernate和Spring中的JPA

[英]Java Spring hibernate and JPA in Spring

What is the benefit of using JPA at the top of hibernate with spring. 使用Spring在hibernate顶部使用JPA有什么好处。 I have seen few projects and few topics on the google where they use JPA + Hibernate + Spring but i could not found good explanation of it. 我在google上看到了很少的项目和几个主题,他们使用JPA + Hibernate + Spring,但我找不到很好的解释。

also individually when to use JPA and when to use Hibernate. 还单独何时使用JPA以及何时使用Hibernate。

I really appreciate if some one help me to under stand 如果有人帮助我站起来,我真的很感激

JPA is just an API, Java Persistence API . JPA只是一个API, Java Persistence API It is the interface for using persistence services. 它是使用持久性服务的接口。 On its own it doesn't do anything but provide metadata. 除了提供元数据之外,它本身没有做任何事情。

Hibernate is an implementation for JPA . HibernateJPA一个实现。 It is an ORM framework. 它是一个ORM框架。 It provides the actual persistence service. 它提供了实际的持久性服务。 It does this by reading the JPA metadata (the annotations). 它通过读取JPA元数据(注释)来实现。

Spring is just a collection of frameworks. Spring只是框架的集合。 One of its frameworks has support for using Hibernate . 其中一个框架支持使用Hibernate For example, when working with databases, you often want to wrap multiple operations in a transaction. 例如,在使用数据库时,通常需要在事务中包装多个操作。 If you only used Hibernate , you would have to keep rewriting code to wrap your operations, but Spring provides transaction management support that does it for you. 如果您只使用Hibernate ,则必须不断重写代码以包装您的操作,但Spring提供了为您执行此操作的事务管理支持。

JPA and Hibernate are the same thing. JPA和Hibernate是一回事。 JPA is the API (interfaces, abstract classes, specification), and Hibernate is one of the implementations of this API. JPA是API(接口,抽象类,规范),而Hibernate是此API的实现之一。

You could use Hibernate and continue using its proprietary, non-standard API, but there's no real advantage, since you can do the same thing using a standard API, that you'll be able to use in other projects, using a different implementation. 您可以使用Hibernate并继续使用其专有的非标准API,但没有真正的优势,因为您可以使用标准API执行相同的操作,您可以使用其他实现在其他项目中使用。

Spring is used as another level, to provide dependency injection, declarative transactions, and much more services. Spring用作另一个级别,用于提供依赖注入,声明式事务和更多服务。

JPA simplifies entity persistence with a standardized ORM programming model. JPA使用标准化的ORM编程模型简化了实体持久性。 In other words, it abstracts the persistence layer in order to cut dependencies between your application code and ORM providers. 换句话说,它抽象了持久层,以便减少应用程序代码和ORM提供程序之间的依赖关系。

This standardization (JPA) provides you with a way to switch between JPA persistance providers such as Hibernate, OpenJPA, etc. 此标准化(JPA)为您提供了一种在JPA持久性提供程序(如Hibernate,OpenJPA等)之间切换的方法。

From Pro Spring 3 (Clarence Ho): 来自Pro Spring 3(Clarence Ho):

JPA defines common set of concepts, annotations, interfaces, and other services that a JPA persistence provider should implement (all of them are put under the javax.persistance package). JPA定义了JPA持久性提供程序应该实现的通用概念集,注释,接口和其他服务(所有这些服务都放在javax.persistance包下)。 When programming to the JPA standard, developers have the option of switching the underlying provider at will, just like switching to another JEE-compliant application server for applications developed on the JEE standards. 在编写JPA标准时,开发人员可以选择随意切换底层提供程序,就像切换到JEE标准开发的应用程序的另一个符合JEE的应用程序服务器一样。

Regarding your other question 关于你的另一个问题

also individually when to use JPA and when to use Hibernate. 还单独何时使用JPA以及何时使用Hibernate。

JPA promotes modularity at the cost of additional complexity. JPA以更高的复杂性为代价促进了模块化。 Whether this trade-off is worth it depends on the complexity of your project. 这种权衡是否值得,取决于项目的复杂程度。

Hibernate closes the gap between the relational data structure in the RDBMS and the Object Oriented model in Java. Hibernate缩小了RDBMS中的关系数据结构和Java中的面向对象模型之间的差距。 This mature and tested library allows you to focus on your business logic using objects and saving time writing boiler-plate code needed to interact with databases. 这个成熟且经过测试的库允许您使用对象专注于业务逻辑,并节省编写与数据库交互所需的样板代码的时间。

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

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