Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 331
Mesut Dogan Avatar asked Jan 08 '23 21:01

Mesut Dogan


1 Answers

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.

like image 147
Mateusz Dymczyk Avatar answered Jan 14 '23 21:01

Mateusz Dymczyk