Is JPA enough to perform CRUD operations

I've been studying JPA and Hibernate for several days. Now I am confused about JPA - Hibernate interactions. I know JPA is a specification and Hibernate implements it. But the missing point is how it works. I mean, in a real application, what does hibernate do and what does jpa do? I am trying to get answers to questions. Let me ask you

  • Question 1: Is JPA only an abstract concept? Does it consist only of interfaces? (I noticed that every package is javax.persistance.)
  • Question 2: Is JPA good enough to perform CRUD operations? (Can JPA be used without Hibernate, etc.). If so, why do we need JPA providers like Hibernate or the like?
  • Question 3: And lastly, I'm looking for something specific. i need to know database interaction -> JPA-> Hibernate. For example, while saving and retrieving something, What is going on in the background that does the database operations (hibernate or jpa) or which one is responsible for providing the db connection?
    I mean, in a jpa / hibernate application, who is responsible for what?
+3


source to share


3 answers


Question 1:

Yes exactly! JPA is just a specification that you already know. It only says what can be done, but does not say how it should be done or does not implement any behavior. You can use JPA annotations with classes javax.persistence

, but at the end nothing happens when your application starts!

Question 2:

As I said above, JPA is just a blueprint for what can be done. Hibernate, OpenJPA, Toplink, etc. They actually implement this specification. They perform operations differently, so there may be speed tradeoffs and so on, but they should all be able to perform the same set of operations specified by JPA. Some may give you more options, but no less.



Question 3:

Again JPA does nothing, it just indicates what can be done. How it is done, how the interaction with the ↔ db code is performed, which SQL queries are created by it with all the specifics of the implementation and will differ (for example, Hibernate can create different SQL queries for the same thing as OpenJPA). How your DB interactions are done at the end is determined at runtime by the (Hibernate) implementation. You can try to find all of this in the docs for a specific implementation. You can also print out the SQL queries that are being executed eg.

You may ask , then why do I need JPA " ? Ok, because you can (in theory!) Change the implementation by simply changing the jar on the path to a different library (ie. From Hibernate to Toplink). In practice, sometimes it is not that easy from -for implementation specifics or how each implementation handles SQL queries, tables, etc.

+6


source


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

1: Yes, fix this technical part of the spec in the form of Java interfaces.

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



3: Interoperability only exists between Hibernate and the database (there are actually other parts like the database driver used, but not against ...).

The idea behind this separation is that you can write code that only uses the interfaces from javax.persistence

. At one point, you (or perhaps a container such as an application server) will define which implementation you want to use. This makes your application very portable and you can choose to be able to switch implementations as you see fit (in theory, in practice it's never that easy ...)

+1


source


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

JPA implementations in popular application servers:

  • Glassfish - EclipseLink (reference implementation)
  • JBoss - Hibernate
  • Websphere - JPA for saving to WebSphere application server, Apache OpenJPA

So when you are using an application server, you can use the JPA interface to perform any CRUD operation with a specific JPA implementation.

Tomcat does not support JPA out of the box. You can use JPA in applications deployed to Tomcat only if those applications embed some JPA implementation. Or use the Apache TomEE project which provides JavaEE functionality for Tomcat.

Remember, Hibernate has a JPA implementation, but also more dependent (cool) features. But if you only use elements from the JPA spec, you can easily switch to a different JPA implementation at any time.

+1


source







All Articles