JDBC:
JDBC (Java Database Connectivity) is a Java API used to connect and execute queries with the database.
JDBC API uses drivers to connect with the database. We can use the API to access tabular data stored in any relational database. Using JDBC API, we can save, update, delete and fetch data from the database.
JDBC COMPONENTS:
Driver Manager: manages the list of drivers in the database.
Driver: manages communications between the application and the database server.
Connection: establishes contact with the database.
Statement: allow submission of the SQL statements to the database.
Result Set: hold the data retrieved from the database (occurs only after executing an SQL query using Statement)
SQL Exception: handles errors in the database application.
ADVANTAGES:
It is capable of reading any database. The only requirement for it to do so is properly installing all the drivers.
It automatically creates the XML format of data from the database.
The conversion of material is not necessary.
It provides full support to query and stored procedures.
Both synchronous and asynchronous processing are supported.
It supports modules.
DISADVANTAGES:
It is susceptible when it comes to the driver. Hence, installing the correct drivers and deploying them for each database type is essential. This is a time taking task and challenging at times.
It does not allow a single sequence to update or insert multiple tables.
It cannot be used in large projects.
Lazy Loading is not supported causing performance issues and blocking system resources.
One must explicitly maintain database connections and transactions.
Creating associations (one-to-one, one-to-many, etc.) between relations is quite hard in JDBC.
Database dependent. You must write different codes for different databases.
HIBERNATE:
Hibernate is a Java framework that simplifies the connection code of a Java application with the database. It is an open source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for data persistence.
ORM: An ORM tool (using the JDBC API internally) simplifies the data creation, manipulation and, access. It is a programming technique that maps the object to the data stored in the database.
JPA: Java Persistence API (JPA) is a Java specification that provides certain functionality and standard to ORM tools. It is a means for an application to persist and retrieve information from a non-volatile storage system.
Non-volatile storage: type of computer memory that can retain stored information even after power is removed.
ADVANTAGES:
Open Source and Lightweight.
Simplified Database Operations.
Fast Performance - cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled by default.
Database Independent Query - HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries.
Automatic Table Creation.
Simplifies Complex Join - Fetching data from multiple tables is easy in Hibernate.
Provides Query Statistics and Database Status.
Lazy Loading is supported.
It internally manages its own transactions.
Associations like one-to-one, one-to-many, many-to-one, and many-to-many are created easily with the help of annotations
DISADVANTAGES:
Performance Cost/Overhead - Hibernate generates lot of SQL statements in runtime based on our mapping , so it is bit slower than JDBC.
Poor performance in Batch Processing.
Not good for small projects.
Does not allow multiple inserts - Doesn't support some queries that are supported by JDBC.
Learning curve.
Increased complexity - additional layers and dependencies make the overall architecture of an application more intricate.
HIBERNATE ANNOTATIONS:
@Entity annotation will mark class as an Entity.
@Table annotation will map the class with provided table. Name attribute can be use to map class name with database table name. It will by default take table name as class name if not provided.
@Id annotation is an Entity identifier
@GeneratedValue use to generate value. This mainly helps to create unique primary key.
IMPORTANT NOTES*
Always make sure to add the correct dependencies in your maven project.
Add a configuration file for your Hibernate project to provide info related to database connection.
Create the entities, schemas and tables with proper naming conventions in your database and classes. Make sure the naming is consistent to prevent runtime errors.
Always catch exceptions in both JDBC and Hibernate projects.
Kommentare