JPA or JDBC, how are they different?

I am learning Java EE and I uploaded an eclipse using glass fish for the same. I saw several examples and also read Oracle docs to learn all about Java EE 5. Connecting to the database was very simple. I opened a dynamic web project, created an EJB session, I used EntityManager and using get methods I could access the saved data table.

In my next project, I created a simple class and then accessed some DB table. The first problem I ran into was that the PersistenceUnit attribute would only be recognized by EJB, Servlet, etc., and not a simple java class. So, I could not use the EntityManager method (or can I?)

I was asked to go through JDBC. The first problem I encountered was connecting to the database. It seems that all this should be hard-coded. I had persistence.xml with which I could easily set up a database connection. Even setting up a driver for the database was easy. There are also no get / set methods in JDBC to access table objects.

How do I understand JPA and persistence regarding JDBC? What was the JPA thinking? Why are there set / get methods? Can someone shed light on the essence of these two and what are the pros and cons without the "jargon"? Please also offer some links. A simple Google search of the differences between JPA and JDBC led me to some sites full of “terminology” that I could not follow :(

+62
java jdbc jpa
Aug 09 '12 at 10:38
source share
4 answers

Layman Terms:

  • JDBC is the standard for database access
  • JPA is the standard for ORM

JDBC is the standard for connecting directly to the database and executing SQL against it - for example, SELECT * FROM USERS, etc. Datasets can be returned that you can process in your application, and you can do all the usual things like INSERTS, DELETES, run stored procedures, etc. This is one of the main technologies underlying most access to the java database (including JPA providers).

One of the problems with traditional JDBC applications is that you can often have some crappy code where there are many comparisons between datasets and objects, the logic mixes with SQL, etc.

JPA is the standard for relational object mapping. This is a technology that allows you to map objects in code and database tables. This can “hide” SQL from the developer, so all they have to do with them are Java classes, and the provider allows you to save them and load them magically. Basically, XML mapping files or annotations on getters, setters can be used to tell the JPA provider which fields on your feature map are in which fields in the database. The most famous JPA provider is Hibernate, so this is a good place for concrete examples.

http://www.hibernate.org/

Other examples include OpenJPA, toplink, etc.

Under the hood, Hibernate and most other JPA providers write SQL and use JDBC to read and write to the database.

+121
Aug 09 2018-12-12T00:
source share

The main difference between JPA and JDBC is the level of abstraction.

JDBC is a low-level standard for interacting with databases. JPA is a higher standard for the same purpose. JPA allows you to use the object model in your application, which can make your life a lot easier. JDBC allows you to do more things with the database directly, but it requires more attention. Some tasks cannot be solved effectively using JPA, but can be solved more efficiently with JDBC.

+30
Aug 09 2018-12-12T00:
source share

JDBC is a much lower level (and earlier) specification than JPA. There is no reason in it, JDBC is an API for interacting with a database using pure SQL - sending queries and receiving results. He has no idea about objects or hierarchies. When using JDBC, you need to translate the result set (essentially the matrix of rows / columns of values ​​from one or more database tables returned by your SQL query) into Java objects.

Now, in order to understand and use JDBC, it is important that you have some understanding and knowledge of SQL. With this, you also get the necessary information about what a relational database is, how you work with it, and concepts such as tables, columns, keys, and relationships. Unless you have at least a basic understanding of databases, SQL, and data modeling, you won’t be able to use JDBC a lot, as it’s actually just a subtle abstraction on top of these things.

+12
Aug 09 '12 at 10:52
source share

JDBC is the forerunner of JPA.

JDBC is the bridge between the Java world and the database world. In JDBC, you need to expose all the dirty data needed for CRUD operations, such as table names, column names, and in JPA (which uses JDBC), you also specify these details of the database metadata, but using Java annotations.

So, JPA creates update requests for you and manages the entities you searched for or created / updated (this also does more).

If you want to make JPA without a Java EE container, then Spring and its libraries can be used with the same Java annotations.

+4
Aug 09 '12 at 10:50
source share



All Articles