Hibernate configuration with annotations

Dilusha Amarasekara
4 min readDec 30, 2020

In this article, I hope to cover the hibernate development process. So, I divide that process into three parts.

· Add the hibernate configuration file.

· Annotate Java class.

· Develop Java code to perform database operations.

Hibernate Configuration File

Basically, the Configuration file tells how to connect hibernate to the database. hibernate uses JDBC to communicate with the database. the configuration file has a database URL, user id, and password. In this hibernate.cfg.xml file can see what are the available data. Also, this file locates the src directory.

Basically, the Configuration file tells how to connect hibernate to the database. hibernate uses JDBC to communicate with the database. the configuration file has a database URL, user id, and password. In this hibernate.cfg.xml file can see what are the available data. Also, this file locates the src directory.

<property name=”connection.driver_class”>com.mysql.cj.jdbc.Driver</property> <propertyname=”connection.url”>jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&amp;serverTimezone=UTC</property>

<property name=”connection.username”>hbstudent</property>

<property name=”connection.password”>hbstudent</property>

Annotate Java Class

Hibernate is a concept of an Entity class. Entity class means java class that is mapped to a database table.

This is the object-to-Relational Mapping(ORM) diagram. Left side java class has fields for the id, first name, last name, and email. Then we want to map this class to the database table(student). So, this class has id, first_name, last_name, and email. So now we look at how to map this class into the table or how to map these fields into the columns. There are two options for mapping.

· XML config file.

· Java Annotations.

XML config file is an old version. Java Annotation is a modern option. So, we talk about option 2 for the annotation. Java annotation concept has two steps.

1. Map class to a database table

2. Map fields to database columns

Step:01

This has two annotation entities and a table. “student” is the name of the database. so same name of the database and table.

Step:02

In this step, we want to add fields into the database columns. In this photo, you can see it easily. It has column annotation. Also, Id is a special annotation(primary key).

This is the Student.java file

@Entity

@Table(name = “student”)

public class Student {

@Id

@Column(name=”id”)

private int id;

@Column(name=”first_name”)

private String firstName;

@Column(name=”last_name”)

private String lastName;

@Column(name=”email”)

private String email;

public Student(){

}

public Student(String firstName, String lastName, String email) {

this.firstName = firstName;

this.lastName = lastName;

this.email = email;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

@Override

public String toString() {

return “Student [id=” + id + “, firstName=” + firstName + “, lastName=” + lastName + “, email=” + email + “]”;

}

}

Develop Java code to perform database operation.

This is the java code setup of the hibernate project.

So here I have, factory=new Configuration().configure then mentioned the hibernate.cfg.xml file then it has addAnnotatedClass() and mentioned Student.class. also this java class is a special annotations on it. Then it has .buildSessionFactory(). So this gives SessionFactory object. That means it can use later on your application.

Then factory.getCurrentSession() gives session object and use it to perform a database operation. Completed java code can see after the “save a java object” section

Save a Java Object

In this code you can see what are the process step by step.

public class CreateStudent {

public static void main(String[] args) {

// TODO Auto-generated method stub

//create session factory-step 01

SessionFactory factory = new Configuration()

.configure(“hibernate.cfg.xml”)

.addAnnotatedClass(Student.class)

.buildSessionFactory();

//create session-step 02

Session session =factory.getCurrentSession();

try {

//create a student object-step 03

System.out.println(“Creating new object….”);

Student tempStudent=new Student(“Dilusha”,”Amarasekara”,”diluchamo.c@gmail.com”);

//start s transaction-step 04

session.beginTransaction();

//save the student object-step 05

System.out.println(“saving the student…”);

session.save(tempStudent);

//commit transaction-step 06

session.getTransaction().commit();

System.out.println(“Done…”);

}finally {

factory.close();

}

}

So, then the data goes to the database table.

thank you !!!

--

--

Dilusha Amarasekara

Undergraduate of University of Moratuwa. Full-Stack Developer