CRUD with Java, Hibernate and MySQL — Part 1
--
This 3-part tutorial explains what Hibernate is, how to set it up to connect with MySQL database and performing common database operations like Create, Read, Update and Delete (CRUD).
Tech Stack:
Java (JDK 1.8)
Hibernate 5.2.11 (Note: Hibernate 5.2 or higher requires JDK 1.8)
MySQL Connector (Java) 5.1.44
Eclipse Spring Tool Suite (3.8.2)
What is Hibernate?
Hibernate is a framework for saving/persisting Java objects to the database. More information about what Hibernate can achieve can be found here.
Why Hibernate?
Hibernate minimizes the amount of JDBC code one needs to develop for connecting to the database as connections are now handled using a hibernate configuration file.
Hibernate handles all the low-level SQL code.
Most importantly, Hibernate provides Object-to-Relational Mapping (ORM), that is, mapping between Java class and database table.
How are Hibernate and JDBC related?
Hibernate uses JDBC for all communications to the database. In other words, Hibernate is an additional layer that uses JDBC for connecting to the database.
Setting up Hibernate in Java Project
For building Hibernate applications in Java, we need the following:
Java Development IDE (can be downloaded here)
Database server (can be downloaded here. For this tutorial, MySQL Community Server should be good.)
Hibernate JAR files (The JAR files can be downloaded here and added to your project build path. We only need jars from the required folder in the downloaded zip file for this tutorial.)
JDBC Driver (The MySQL Connector can be downloaded here and added to your project build path.)
This is how the final project set up looks like:
The installation instructions for MySQL can be found here. The MySQL community server runs on localhost:3306 by default. For the purpose of this tutorial, we create a new user: hb_student
Now, we create a database table named “student” by running the following SQL query:
CREATE DATABASE IF NOT EXISTS `hb_student_tracker` ;
USE hb_student_tracker;DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
This is how our “student” table schema looks like:
Lets set up our hibernate configuration and Object-Relational mapping in Part 2 of this tutorial.
Credits: The example in this article was inspired from a Spring & Hibernate For Beginners course by Chad Darby on Udemy.
Disclaimer: The aim of this article is to explain a complex topic in a short time for the reader and is in no way, shape or form intended towards discrediting the instructor’s work.
Do you wish to improve your problem-solving skills for coding interviews? Do you struggle to apply data structures and algorithms while solving a problem? Sign up for AlgoExpert.io. I can personally attest that having gone through the questions and video explanations, they are the best resource for coding interview preparation on a tight time frame.
Use promo code: nwpxi-35 to get a 30% discount on AlgoExpert 1-year subscription.