Open In App

Spring Hibernate Configuration and Create a Table in Database

Last Updated : 30 Nov, 2022
Like Article

Spring Boot is used to develop REST web services and Microservices. Spring Boot reduces the configuration and setup time required for spring projects. Hibernate (Java ORM Framework) provides a framework for mapping an object-oriented domain to a relational database table like MySQL, Oracle, and PostgreSQL. Spring Boot Hibernate is used in the java environment and provides a solution for object relation mapping that use to map object-relational databases to the spring boot application.  Spring Boot Hibernate is an open-source framework used to develop the Java application and is implementing the Java persistence API specification for the persistence data. Hibernate Query Language is used to generate the database query, so there is no need to do it manually at the time of implementation in the project. Spring Boot Hibernates helps to create the table automatically in the database without doing it manually.

object-oriented to relational database

object-oriented to relational database

What are the Advantages of Hibernate?

  • Easy to perform CRUD operation on data from multiple tables.
  • Mapping Java classes with database tables by using an XML file.
  • To make changes in the database we only need to make changes in the XML file.
  • no need for any server to operate the project while using hibernate

Hibernate supports the below following RDMS database engines:

  • MySQL
  • Oracle
  • DB2
  • Sybase
  • Informix
  • PostgreSQL

How to Configure Hibernate using MySQL

Before starting with the steps create a Maven project. After creating the project looks like the below. 


Now step-up the workbench and create a new schema. The created schema looks like this.


Step by Step Implementation

Step 1: Now open the file in eclipse to enter all the database configurations.


Add database configuration in the file under the src/main/resources folder.

#database configurations

brief about database configuration first add the URL of a database with a port number then add the username and password of the database then add MySQL data source.

Step 2: After adding database configuration add JPA (Java Persistence API) configuration for connectivity with the database using hibernate.

#jpa configuration

brief about JPA configuration first add the properties of which relational database are we going to use, and set changes in the database will be auto-update then set true to show the SQL query on the console each time the project will run if you want to see the query should be in formate then set the properties true for formate.

Step 3: Add the dependency for MySQL in the pom.xml file


Step 4: Create an Entity package to create a table in the database. The below example shows to create package and Java files accordingly.


Step 5: Now add the variables and getter setter methods in the file using annotations to map the Java file to a database table. 


package com.exam.Portal.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "users")
public class User {
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private String email;
    private String phone;
    private boolean enable;
    public User() {
    public User(int id, String username, String password, String firstname, String lastname, String email, String phone,
            boolean enable) {
        super(); = id;
        this.username = username;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname; = email; = phone;
        this.enable = enable;
    public int getId() {
        return id;
    public void setId(int id) { = id;
    public String getUsername() {
        return username;
    public void setUsername(String username) {
        this.username = username;
    public String getPassword() {
        return password;
    public void setPassword(String password) {
        this.password = password;
    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) { = email;
    public String getPhone() {
        return phone;
    public void setPhone(String phone) { = phone;
    public boolean isEnable() {
        return enable;
    public void setEnable(boolean enable) {
        this.enable = enable;
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", firstname=" + firstname
                + ", lastname=" + lastname + ", email=" + email + ", phone=" + phone + ", enable=" + enable + "]";


Now if you are going to run the main file of project tables will going to create in the database. Like below snapshot


Hibernate is an object-relational mapping solution that is used to map object-oriented/Java classes to object-relational databases. It reduces the work of the developer because tables going to create automatically without writing any query and also provides API for retrieving java objects directly from the database server and applications will minimize access to the database.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads