⟩ Explain me what are the advantages of Hibernate over JDBC?
Some of the important advantages of Hibernate framework over JDBC are:
☛ Hibernate removes a lot of boiler-plate code that comes with JDBC API, the code looks more cleaner and readable.
☛ Hibernate supports inheritance, associations and collections. These features are not present with JDBC API.
☛ Hibernate implicitly provides transaction management, in fact most of the queries can’t be executed outside transaction. In JDBC API, we need to write code for transaction management using commit and rollback.
☛ JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch block code. Most of the times it’s redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException un-checked exception, so we don’t need to write code to handle it. Hibernate built-in transaction management removes the usage of try-catch blocks.
☛ Hibernate Query Language (HQL) is more object oriented and close to java programming language. For JDBC, we need to write native sql queries.
☛ Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.
☛ Hibernate provide option through which we can create database tables too, for JDBC tables must exist in the database.
☛ Hibernate configuration helps us in using JDBC like connection as well as JNDI DataSource for connection pool. This is very important feature in enterprise application and completely missing in JDBC API.
► Hibernate supports JPA annotations, so code is independent of implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application.