×
☰ See All Chapters

JPA vs JDBC

Below table list down the differences between JPA and JDBC, also advantages of using JPA over JDBC:

JPA

JDBC

JPA is data base independent, single query will work for all ORACLE, MySQL, SQLServer etc. If we change the database, we need to change just the provider in configuration file.

In case of JDBC, query must be data base specific. . If we change the database, we need to change all the database queries.                                                                                                                                                                    

JPA supports caching, two level of cache. First level and 2nd level. So we can store the data into Cache for better performance.

In case of JDBC we need to implement our java cache, and there is no built in cache support.                                                                                                                                              

No need to create any connection pool in case of JPA.

In case of JDBC we need to create our own connection pool.

As JPA is set of Objects, we don't need to learn SQL language. We can treat TABLE as an Object. Only Java knowledge is need.

In case of JDBC, we need to learn SQL

 

Don't need Query tuning in case of JPA. If we use Criteria Quires then JPA automatically tunes the query and return best result with performance.

In case of JDBC we need to tune your queries                                                                                                                                                                              

Productivity is good, because we don’t need to write queries

Development is slower.

In the xml file you can see all the relations between tables in case of JPA. Easy readability.

Using JDBC we cannot see all the relations between tables.

In JPA if we fetch the data from parent table, data from child table (which is related using foreign/primary key) will be fetched automatically.

JDBC Don’t have such support

 

 

Why do we need a persistence framework

To avoid writing Database specific SQL queries into our Java applications. If we use JDBC, we should be familiar with relational database SQL syntax. Java JDBC work is tedious and diverts programmer attention from programming business logic. Programmers should be careful themselves with binding query parameters, writing SQL, executing queries, scrolling the result set and retrieving the required data. Java programs are object-oriented (or at least should be) in nature whereas relational databases are tabular in nature. This complicates the object-relational mapping and forces developers to think about problems in two different ways. A persistent framework makes coding faster and improves maintenance. The persistent framework can also help to isolate the application from database specific intricacies. Finally, a persistent framework can, in some cases, improve performance with appropriate performance enhancements and caching.

Advantages of JPA over JDBC

  1. JPA Supports POJO-POJI model Programming 

  2. JPA is Light –Weight technology to develop DB independent persistence logic. 

  3. JPA Allows to work with any JAVA, JEE framework softwares based applications to make them interacting with DB software. 

  4. When we want to do distribute transactions in JDBC we have to implement JTA API (Java Transaction API). But JPA provides built in transactions management, connection pooling. 

  5. JPA supports two levels of caching (or) Buffering to reduce network round trips between client applications and Database. 

  6. JPA provides JQL (JPA Query Language) as Database independent language to perform persistence operations. 

  7. From Entity classes (we will read about entity classed in coming chapters) we can see all the relations between tables. Easy readability 

  8. JPA Supports automatic versioning of rows but JDBC Not. 

  9. JPA allows object level relationship in development of persistence logic, when tables are there in relationships like 1-1,1-n,n-n etc. JPA uses fetching strategy for retrieving associated objects if the application needs to navigate the association. You can navigate object relationships transparently. Related objects are automatically loaded as needed. For example if you load a PO (Purchase Order) and you want to access its Customer, you can simply access PO. The ORM will take care of loading the Customer data for you without any effort on your part. 

  10. When we are using Data source connection, in JDBC we have to implement JNDI API Java Naming and Directory Interface API ). But JPA provides built in support for Data source connection. 

 


All Chapters
Author