Overview on Java Persistence using JDBC, JPA,Hibernate and JDBI

Overview on Java Persistence using JDBC, JPA,Hibernate and JDBI

Data persistence is one of the most critical parts when it comes to N-tier applications and applications in general, in simple terms, persisting your data means storing them in your data store or to put it more simply and for understanding, in a database.

You don't want your data to be lost or you will cause a disaster in your application, imagine if Facebook loses their data which they use for their tailored customized ads, the next day the whole world would be talking about it and it will cause them billions of dollars( that most probably won't happen).

If you are a java developer then most probably you have heard of or used JDBC (Java DataBase Connectivity) to store and retrieve your data, and at some point you heard or used Hibernate framework, and then again, heard about JPA(Java Persistence API), JDBI (Java Database Interface) so how are these different and how are these similar.

If you understand the bigger picture then its easier for you to choose and use them so lets see what are they:

The difference between JDBC and JPA:

JDBC is a standard for database access, its standard for performing SQL :

("SELECT * FROM EMPLOYEES")

Its an API and its only an interface, meaning that its just specifications and abstraction. On top of this Specifications, the vendors such as Oracle, MySql provide implementations to the different database functionalities. For example the implementation of the Insertion of a row in the database for Oracle database differs from the one in MySQL and that is left for the vendor to implement:aSo simply JDBC is the standard(interface) for the database access and on of top it, it has an implementation provided by different vendors.

The Concept of JDBC API and its implementation

JPA on the other hand is a standard(Interface) for ORM(Object Relational Mapping), its the standard that allows you to persist your data as Java objects. So instead of performing pure SQL on your database:

INSERT INTO EMPLOYEES
VALUES('Carry', 'Finance');

In JPA you deal with objects, the JPA will do the mapping to your database table and the persistence,, it will look something like:

Employee emplyee= new Employee();

employee.setName("Carry");

emplyee.setDepartment("Finance");

entityManager.persist(employee);

entityManager.getTransaction.commit();

so what happens here is that, the JPA implementations map between your database table (in this case 'EMPLOYEES') and your object Employee and when you commit, it generates the SQL or HQL for you to deal with the database.

so you can see, the JPA looks more like java syntax and instead of dealing with SQL directly, the JPA implementation will take care of that for you and you can focus on your business logic and on your java objects only instead of mixing SQL with your java code.

I want you to keep in mind that, JPA is only the standard, the implementation will be provided by some other vendor/framework such as Hibernate.

So basically the difference here between JDBC and JPA is the level of abstraction in both, in one you work with low level SQL and in the other, that is taken care of and abstracted for you (Higher level) but they are both standards(interfaces).

Hibernate:

How does Hibernate come into the picture? so the same way the vendors come up with implementations in the case of JDBC, Hibernate is an implementation of JPA. its basically a framework uses the standards in JPA to make you persists your objects.

So basically Hibernate is a JPA implementation.

IMPORTANT: Keep in mind that, if you are using JDBC and pure SQL, which is a low level of abstraction, then most probably you are going to have a better performance because you will be talking to the database directly but in the case of JPA or Hibernate , performance might not be as good as JDBC, not to say that you can't have a good performance using Hibernate. In addition Hibernate offers that ORM and the Java like syntax for persistence.

Now what is JDBI and how is it related?

So we have the low level abstraction JDBC & SQL in one side , and the higher level abstraction JPA & Hibernate in the other side, I would say JDBI sits in between but more closer to JDBC in terms of abstraction.as the owner of JDBI describes it :

"JDBI is a SQL convenience library for Java. It attempts to expose relational database access in idiomatic Java, using collections, beans, and so on, while maintaining the same level of detail as JDBC"

So basically in JDBI, you still use SQL like JDBC but its more tailored and improves the JDBC's rough interface. Unlike an ORM, JDBI doesn't aim to provide a complete object relational mapping framework - instead of that hidden complexity that is taken care of by frameworks such as Hibernate, it provide building blocks that allow you to construct the mapping between relations and objects as appropriate for your application which means a better performance but in a better API.

The Syntax for JDBI looks like:

SqlUpdate("INSERT INTO EMPLOYEES (name, department) VALUES (?, ?)" ) 

void insertEmployee(String name, String department);

I would simply put it, it has the benefits of both worlds. but instead of jumping into one, its better you understand your requirements and make a decision from there.



Felipe Franco

Data Science | Machine Learning | Artificial Intelligence

3 年

Claudia Nogueira dos Anjos

要查看或添加评论,请登录

Sabir Moglad的更多文章

社区洞察

其他会员也浏览了