Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a JPA implementation?

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

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?

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

like image 528
user262525 Avatar asked Dec 18 '10 07:12

user262525


People also ask

What is JPA used for?

The Java™ Persistence API (JPA) provides a mechanism for managing persistence and object-relational mapping and functions since the EJB 3.0 specifications. The JPA specification defines the object-relational mapping internally, rather than relying on vendor-specific mapping implementations.

What is JPA in simple terms?

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database. JPA was defined as part of the EJB 3.0 specification as a replacement for the EJB 2 CMP Entity Beans specification.

Who provides JPA implementation?

The JPA, short for Java Persistence API, is part of the Java EE 5 specification and has been implemented by Hibernate, TopLink, EclipseLink, OpenJPA, and a number of other object-relational mapping (ORM) frameworks.

What is JPA and use of JPA?

JPA stands for Java Persistence API (Application Programming Interface). It was initially released on 11 May 2006. It is a Java specification that gives some functionality and standard to ORM tools. It is used to examine, control, and persist data between Java objects and relational databases.


2 Answers

JPA

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

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.

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.

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.

Popular implementations include Hibernate, EclipseLink, OpenJPA and others.

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

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

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

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.

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

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.

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.

like image 113
darioo Avatar answered Sep 28 '22 01:09

darioo


It's the same as Java, it has a specification (Java SE, Java EE) and implementation, most people use the reference implementation (By Sun / Oracle) which is a blueprint for others to make their own "better" and "advanced" implementation of the specification (APIs, Interfaces and documentation / JavaDoc if you need a one word definition, although it is much more than that)

So the Java / JDK you are most likely using is just an implementation (that is very popular). There are many more implementations out there, some have more Garbage Collection configuration options, some claim to be faster, and some are just not worth using.

So the implementation basically does everything, it has the code, where the JPA is an API (implied by it's name, Java Persistance API) e.g. again, more about interfaces and documentation than real implentation of the interface.

Think of it as the abstact class that Hibernate's JPA implementation / EclipseLink extends / implements

Regarding JPA, as opposed to Hibernate which has a single implementation, the idea is to decouple the interface from the implementation, so that the "best" implementation will win and that everyone can participate in the game, not just the interface creator.

This allows for instance for Google App Engine to support JPA, as it just needs to follow an API (DataNucleus is the implementation) if it has to use Hibernate, it would have required to modify the Hiberanate release to support Google's Big Table format (the fact that JPA in GAE is not that great is another story)

like image 35
Eran Medan Avatar answered Sep 28 '22 00:09

Eran Medan