CRUD with Java, Hibernate and MySQL — Part 2

In Part 2 of this tutorial, we set up hibernate configuration and use annotations for Object-Relational Mapping (ORM). For more information about hibernate and how to set it up for your project, please refer to Part 1 of this tutorial.

The Hibernate configuration file handles JDBC connections to the MySQL database. In this file, we set up the session factory to retrieve session objects for connecting to the database. Also, we need to specify the JDBC driver, JDCB url, username, password and SQL dialect.

<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory> <! — allows us to get session objects for connecting to the database --><! — JDBC Database connection settings -->
<property name=”connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”connection.url”>jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false</property>
<property name=”connection.username”>hbstudent</property>
<property name=”connection.password”>hbstudent</property>
<! — JDBC connection pool settings … using built-in test pool -->
<property name=”connection.pool_size”>1</property>
<! — Select our SQL dialect -->
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>
<! — Echo the SQL to stdout -->
<property name=”show_sql”>true</property>
<! — Set the current session context -->
<property name=”current_session_context_class”>thread</property>

</session-factory>
</hibernate-configuration>

Now, we create an Entity class that maps to the database table and annotate it with the @Entity and @Table annotation. @Table annotation specifies the name of the database table.

package com.luv2code.hibernate.demo.entity;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name=”student”)
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@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 + “]”;
}
}

In our case, the entity class is called “Student”. We map the fields to the columns in the database table using the @Column annotation.

@Id is used to specify the primary key in the table.

@GeneratedValue annotation specifies the strategy for how the id should be auto incremented. In our case we specify, strategy=GenerationType.IDENTITY indicating auto-increment by 1. More strategies depending on requirements for auto-increment can be used and can be found here.

In Part 3 of this tutorial, we will dive into using Hibernate for performing CRUD database operations.

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.

Full-Stack Senior Software Engineer | AWS Certified Developer Associate | AWS Certified Solution Architect Associate | Technologist | Blogger

Full-Stack Senior Software Engineer | AWS Certified Developer Associate | AWS Certified Solution Architect Associate | Technologist | Blogger