Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaSpring Hibernate Configuration and Create a Table in Database

Spring Hibernate Configuration and Create a Table in Database

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 application.properties file in eclipse to enter all the database configurations.

 

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

#database configurations
spring.datasource.url=jdbc:mysql://localhost:3306/examportal
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class=com.cj.jdbc.Driver

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
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.formate_sql=true;

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

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>

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 User.java file using annotations to map the Java file to a database table. 

Java




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;
 
@Entity
@Table(name = "users")
public class User {
 
    @Id
    @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();
        this.id = id;
        this.username = username;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.phone = phone;
        this.enable = enable;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.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) {
        this.email = email;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public boolean isEnable() {
        return enable;
    }
 
    public void setEnable(boolean enable) {
        this.enable = enable;
    }
 
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", firstname=" + firstname
                + ", lastname=" + lastname + ", email=" + email + ", phone=" + phone + ", enable=" + enable + "]";
    }
 
}


Output:

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.

RELATED ARTICLES

Most Popular

Recent Comments