JDBC vs JPA vs Spring Data JPA? So, what are they and how are they related.
Before we dive into it, let’s talk about how the application communicates with database. Most of the application uses database to store information related to application that can be used further. Database connection is not an easy task. It is a process where you need to consider different factors like data access layer, mapping between database table and Java Object and connection pool to improve application performance.
As you can see there are different stages, we need to cover to accomplish our goal to connect to database successfully and efficiently. Fortunately, there are different libraries that makes this entire process easy and less time consuming. JDBC, Spring Data JPA, hibernate are few of examples among them. They help to write and execute queries to connect to database.
JDBC Overview
JDBC is an interface or an API that allows an application to communicate with relational database. It is the very first thing we need to connect to database. If you are using JDBC, then you have whole lot of tasks to be done.
There is a bunch of lines of code to simply connect to database: once the setup is done, you need to map the database fields to Java class fields. Writing one queries is not rough but think if you have to write multiple queries and the tables, fields is large then the entire process is inefficient. For example, you need to update a field then you have to walk through every code where you have used that table which does not sound good as a developer. As we are always looking to write, develop stuff efficiently.
JPA Overview
JPA is a standard or specification that allows us to map a Java object and database table. Having said that, it simplifies the development process that JDBC was having difficulty with.
In JPA, you need to define table and its fields too but only in a single place through the use of annotation. By the way JPA also use JDBC but it is under the hood.
It is a specification not an implementation, just like Java Interface, and we need to implement the interface. Similarly, JPA needs an implementation. Some other framework has to be used along side JPA. JPA sets the rules or boundaries how database connection can be achieved, and the other framework uses it to make implementation successful.
Some popular framework are Hibernate, iBatis etc. Hibernate is the most popular ORM (Object Relational Mapping) framework. By now, you might me thinking we already know the difference and just use JPA. However, let’s take some time to dive in the differences.
Differences between JDBC vs JPA
Boiler plate clean up
JPA gets rid of all the boiler plate code and handling time-consuming code for the mapping.
With JPA unlike JDBC, you do not need to write the entire query but a simple annotation on the top of table. That’s a way how two tables will communicate with each other. Look, this is easy. All the associations like one-to-one, many-to-one etc. can be easily implemented.
@Entity
@Table(name = "pages")
public class Page implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@OneToMany(mappedBy = "page", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
private Set<Post> posts;
public Page() {
}
public Page(String title) {
this.title = title;
}
// getters and setters, equals(), toString()
}
Database Dependent
JDBC is database dependent whereas JPA is database agnostic. For JDBC if you want to change database you have to write all or most the scripts again which is not the case with JPA. The code change will be minimum or very not at all.
Exception Handling
One painful thing while using JDBC is that it throws checked exception like SQLException and for checked exception, we have to write the queries int try-catch block. Just imagine having try catch block everywhere in the code which will make me mad atleast. But with JPA only unchecked exception is thrown. Hence, we will have a clean code with no try-catch block statements.
Transaction Management
Transaction management needs to handled explicitly with JDBC with commit and rollback statements whereas JPA provides implicit handling of transaction.
Why Spring Data JPA?
While building a java application, the cumbersome part has been implementing a data access layer. There is too much of boilerplate code to execute even simple queries, pagination and so on. Spring Data JPA comes to the rescue; it significantly improves the implementation of data access layers by reducing the amount of boiler plate code and hence reducing the effort and letting the developer aim at writing the business logic code.
It is most popular library for communicating with database. It adds one more layer on top of JPA implementation. By default, Data JPA uses Hibernate as the provider. While using spring data JPA you do not need to worry about most of the stuff and simply need to provide the database connection point like host, username, password and so on.
Spring Data JPA auto configures all the necessary code requires for database connections.
Conclusion
In this post we learn the difference between JDBC, JPA and Spring JPA. Also, how Spring Data JPA used JPA under the hood and JPA uses JDBC. Spring Data JPA is like a layer trying to facilitates all the possible solutions to database connection with minimum effort.