Introduction To Hibernate - Notes Lyst8855
Introduction To Hibernate - Notes Lyst8855
Introduction to Hibernate
Student table
roll name email
1 alex alex@gmail.com
Similarly
Student table
roll name email
1 alex alex@gmail.com
2 bob bob@gmail.com
3 charli charli@gmail.com
In JDBC we have already seen how we can store data into the database from the java program and
fetch data from the database to the java program.
But now we will see how we can perform Object to Relational Mapping and to achieve this we have
ORM(Object to Relational Mapping) tools and ORM is a technique for converting data between Java
objects and relational databases (table). In simple words, we can say that the ORM implements the
responsibility of mapping the object to the relational model and vice-versa. The ORM tool does
mapping in such a way that the model class becomes a table in the database and each instance
becomes a row of the table.
There are many ORM tools available in the market as shown below-
● Hibernate.
● TopLink.
● EclipseLink.
● OpenJPA.
● MyBatis (formally known as iBatis)
And we will be using Hibernate, as it is the most popular one.
Before we start with Hibernate, there are a few prerequisites that should be met. So make sure all the
below concepts are completed before you start with hibernate-
● Core Java
● JBDC
● MySQL
Hoping that you have met all the prerequisites, we will proceed to installing/ setting up Hibernate in
our java project.
Step1:
1. Create a new Java project-
2. Provide a name for the project and click on the “Don’t create” module
3. Create a package “com.tapacad.application”
4. Create a class inside the package.
Step2:
Let us now download all the different JARs
4. Select the latest version and click on the download button. You will be redirected to
another page and the download will begin
5. Unzip the downloaded RAR file
6. Download MySQL Connector from the browser and extract it into a separate folder
a. Select JDBC Driver for MySQL
12. Select all the JAR files in the lib folder and click on the “Apply and close” button
Step3:
a. Create a database called “hibernatedb” inside the MySQL workbench
Student.java
public class Student {
//Attributes
//Constructors
public Student() {
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver.class">com.mysql.cj.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</propert
y>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property
name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Key Value
hibernate.connection.driver.class com.mysql.cj.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/hibernatedb
hibernate.connection.username root
hibernate.connection.password root
dialect org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto create/update
package com.tapacad.application;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.tapacad.model.Student;
config.configure();
config.addAnnotatedClass(Student.class);
// Logics
But when we execute the code, we get an exception as we didnt mention that which table it should
connect to in the database.
So we have to make use of Annotations to resolve the error-
Modify the Student class as shown below-
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "student")
public class Student {
//Attributes
@Id
@Column(name = "roll")
private int roll;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
Eventhough the program executed successfully, the data is not added in the database as we did not
commit the transaction.
So let us create an transaction object in the java program.
Program1.java
package com.tapacad.application;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tapacad.model.Student;
config.configure();
config.addAnnotatedClass(Student.class);
// Logics
transaction.commit();
}
Even now, if you are not getting the output then, please change the dialect in the
configuration file to -
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>