Mesut Dogan
Mesut Dogan

Reputation: 592

Is JPA enough to perform CRUD operations

For a few days, I have been researching JPA and Hibernate. Now I got confused about JPA - Hibernate interactions. I know JPA is a specifcation and Hibernate implements it. But the missing point is a working principles . I mean in a real application, what does hibernate do and what does jpa do ? I am trying to get below questions. Let me ask you;

  1. Question 1 : Is JPA only abstract notion ? Does it consist of only interfaces? (I have observed that everyting is interafce in javax.persistance package.)
  2. Question 2 : Is JPA enough to perform CRUD operations ? (Is it possible to use JPA without Hibernate or etc.) If it does , why we need JPA providers such as Hibernate or etc. ?
  3. Question 3 : And last one , I am searching for concrete something. I need to know Database->JPA->Hibernate interaction. For example while Saving and fetching something , What does happen in background ?which one is performing database operations(hibernate or jpa) or which one is responsible for providing db connection ?
    I mean, in jpa/hibernate based application , who is responsible for what?

Upvotes: 5

Views: 510

Answers (3)

Mathias Begert
Mathias Begert

Reputation: 2470

As you mentioned yourself: JPA is a specification, Hibernate an implementation!

1: Yes, correct this is the technical part of the specification in form of Java Interfaces.

2: NO, JPA is not "enough", JPA can't do anything, it is just a specification.

3: An Interaction exists only between Hibernate and the Database (actually, there are other parts like the database driver involved, but don't mind...).

The idea behind this separation is you can write code that only uses the interfaces from javax.persistence. At one single place you (or maybe a container like an application server) define which implementation you want to use. This makes your application very portable, and you could choose to switch implementations as you like (theoretically, in practice it is never that easy...)

Upvotes: 1

Patrycja Rychlik
Patrycja Rychlik

Reputation: 61

As you state JPA is just a specification, so there is no implementation but every Java EE container should support it ( implementation of JPA is included in application server ).

JPA implementations in Popular application servers :

  • Glassfish - EclipseLink (reference implementation)
  • JBoss - Hibernate
  • Websphere - JPA for WebSphere Application Server persistence, Apache OpenJPA

So when you are using application server you can use JPA interface to perform any CRUD operation through particular JPA implementation.

Tomcat does not support JPA out-of-the-box. You can use JPA in applications deployed on Tomcat only if these applications embed some JPA implementation. Or use Apache TomEE project which provide JavaEE features for Tomcat.

Remember that Hibernate has JPA implementation but also more addictional (cool) features. But if you are using only elements from JPA spec you can anytime easily switch to another JPA implementation.

Upvotes: 1

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

Question 1:

Yes, exactly! JPA is just a specification as you already know. It only says what can be done but doesn't say how it should be done or doesn't implement any behaviour. You can use JPA annotations with javax.persistence classes but in the end when you run your application nothing will happen!

Question 2:

As I said above, JPA is just a blueprint saying what can be done. Hibernate, OpenJPA, Toplink etc. actually implement that specification. They perform the operations differently so there might be tradeoffs in speed etc. but all of them have to be able to perform the same set of operations specified by JPA. Some might give you more features but never less.

Question 3:

Again JPA isn't performing any actions, it just specifies what can be done. How it's done, how the code <-> db interaction is performed, what kind of SQL queries are created it's all implementation specific and will differ (for instance Hibernate might create different SQL queries for the same thing than OpenJPA). How your DB interactions are performed in the end is determined during runtime by the implementation (Hibernate). You can try to find it all in the documentations for the concrete implementation. You can also print the SQLs that are performed for instance.

You might ask "then why do I need JPA"? Well that's because you can (in theory!) change the implementation just by changing the jar on the classpath to a different library (i.e. from Hibernate to Toplink). In practice sometimes it's not that easy due to implementation specific features or how each implementation handles SQL queries, tables etc.

Upvotes: 8

Related Questions