1. Overview
In this tutorial, we're going to look at the differences between the Java Database Connectivity (JDBC) API and the Java Persistence API (JPA).
2. What Is JDBC
JDBC is a programming-level interface for Java applications that communicate with a database. An application uses this API to communicate with a JDBC manager. It's the common API that our application code uses to communicate with the database. Beyond the API is the vendor-supplied, JDBC-compliant driver for the database we're using.
3. What Is JPA
JPA is a Java standard that allows us to bind Java objects to records in a relational database. It's one possible approach to Object Relationship Mapping(ORM), allowing the developer to retrieve, store, update, and delete data in a relational database using Java objects. Several implementations are available for the JPA specification.
4. JPA vs JDBC
When it comes to deciding how to communicate with back-end database systems, software architects face a significant technological challenge. The debate between JPA and JDBC is often the deciding factor, as the two database technologies take very different approaches to work with persistent data. Let’s analyze the key differences between them.
4.1. Database Interactions
JDBC allows us to write SQL commands to read data from and update data to a relational database. JPA, unlike JDBC, allows developers to construct database-driven Java programs utilizing object-oriented semantics. The JPA annotations describe how a given Java class and its variables map to a given table and its columns in a database.
Let's see how we can map an Employee class to an employee database table:
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@Column(name = "employee_name")
private String employeeName;
}
The JPA framework then handles all the time-consuming, error-prone coding required to convert between object-oriented Java code and the back-end database.
4.2. Managing Associations
When associating database tables in a query with JDBC, we need to write out the full SQL query, while with JPA, we simply use annotations to create one-to-one, one-to-many, many-to-one, and many-to-many associations.
Let's say our employee table has a one-to-many relationship with the communication table:
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@OneToMany(mappedBy = "employee", fetch = FetchType.EAGER)
@OrderBy("firstName asc")
private Set communications;
}
The owner of this relationship is Communication, so we're using the mappedBy attribute in Employee to make it a bi-directional relationship.
4.3. Database Dependency
JDBC is database-dependent, which means that different scripts must be written for different databases. On the other side, JPA is database-agnostic, meaning that the same code can be used in a variety of databases with few (or no) modifications.
4.4. Exception Handling
Because JDBC throws checked exceptions, such as SQLException, we must write it in a try-catch block. On the other hand, the JPA framework uses only unchecked exceptions, like Hibernate. Hence, we don't need to catch or declare them at every place we're using them.
4.5. Performance
The difference between JPA and JDBC is essentially who does the coding: the JPA framework or a local developer. Either way, we'll have to deal with the object-relation impedance mismatch.
To be fair, when we write SQL queries incorrectly, JDBC performance can be abysmally sluggish. When deciding between the two technologies, performance shouldn't be a point of dispute. Professional developers are more than capable of producing Java applications that run equally well regardless of the technology utilized.
4.6. JDBC Dependency
JPA-based applications still use JDBC under the hood. Therefore, when we utilize JPA, our code is actually using the JDBC APIs for all database interactions. In other words, JPA serves as a layer of abstraction that hides the low-level JDBC calls from the developer, making database programming considerably easier.
4.7. Transaction Management
In JDBC, transaction management is handled explicitly by using commit and rollback. On the other hand, transaction management is implicitly provided in JPA.
5. Pros and Cons
The most obvious benefit of JDBC over JPA is that it's simpler to understand. On the other side, if a developer doesn't grasp the internal workings of the JPA framework or database design, they will be unable to write good code.
Also, JPA is thought to be better suited for more sophisticated applications by many developers. But, JDBC is considered the preferable alternative if an application will use a simple database and we don't plan to migrate it to a different database vendor.
The main advantage of JPA over JDBC for developers is that they can code their Java applications using object-oriented principles and best practices without having to worry about database semantics. As a result, development can be completed more quickly, especially when software developers lack a solid understanding of SQL and relational databases.
Also, because a well-tested and robust framework is handling the interaction between the database and the Java application, we should see a decrease in errors from the database mapping layer when using JPA.
6. Conclusion
In this quick tutorial, we explored the key differences between JPA and JDBC.
While JPA brings many advantages, we have many other high-quality alternatives to use if JPA doesn’t work best for our current application requirements.
The post A Comparison Between JPA and JDBC first appeared on Baeldung.