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.
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:
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.
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.