d-man
d-man

Reputation: 58103

Java Spring hibernate and JPA in Spring

What is the benefit of using JPA at the top of hibernate with spring. 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.

also individually when to use JPA and when to use Hibernate.

I really appreciate if some one help me to under stand

Upvotes: 1

Views: 718

Answers (3)

Ulises
Ulises

Reputation: 13429

JPA simplifies entity persistence with a standardized ORM programming model. In other words, it abstracts the persistence layer in order to cut dependencies between your application code and ORM providers.

This standardization (JPA) provides you with a way to switch between JPA persistance providers such as Hibernate, OpenJPA, etc.

From 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). 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.

Regarding your other question

also individually when to use JPA and when to use Hibernate.

JPA promotes modularity at the cost of additional complexity. 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. 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.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692121

JPA and Hibernate are the same thing. JPA is the API (interfaces, abstract classes, specification), and Hibernate is one of the implementations of this 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.

Spring is used as another level, to provide dependency injection, declarative transactions, and much more services.

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280138

JPA is just an 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. It is an ORM framework. It provides the actual persistence service. It does this by reading the JPA metadata (the annotations).

Spring is just a collection of frameworks. One of its frameworks has support for using 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.

Upvotes: 1

Related Questions