Hibernate – Many-to-One Mapping
Hibernate is an open-source, ORM(Object Relational Mapping) framework that provides CRUD operations in the form of objects. It is a non-invasive framework. It can be used to develop DataAcessLayer for all java projects. It is not server-dependent. It means hibernate code runs without a server and with a server also. It is a top layer of JDBC, JTA, and JNDI Technologies. It follows the ORM concept
- ORM is a theoretical concept
- It makes programming or development is in the object orientation process
Hibernate supports object-orientation programming by using the mapping concepts. This mapping should be done by the programmer then hibernate will insert the objects into the Database table as a row and the row can be converted back to an object without any SQL query written by the programmer
Associations in Hibernate
Parent class must have child class reference with or without collection type. This association can also follow multiplicity as
Here these are divided into two types they are
Here we are going to discuss Many-To-One mapping developed by using SpringBoot Data JPA in STS (Spring Tool Suite)
SpringBoot is an extension of the spring-based framework which eliminates the boilerplate configuration required for the setup spring-based application. It is basically built on top of the spring framework to basically reduce a lot of configuration required to set up a spring-based application. The main purpose of SpringBoot is to reduce the common code written by programmers which is called Autoconfiguration. Data JPA provides @NoRepositoryBean which is autoconfigured and self logic implemented for basic operations. In simply JPA (Java Persistence API) is an interface Hibernate provides the implementation for the interface methods
In this article, we will discuss Many-To-One mapping with help of one example. Many employees work in the same company so all the working employees have the same company address. Many employees have one address
Steps To Creating Spring Boot Project Using STS
Step 1: Open the STS (Spring Tool Suite)
Step 2: Click on the File menu –>select New–>select Spring Starter Project then a new window is opened that provides the required details like Name, Group, Artifact, java version, etc.
Step 3: Enter Project name, Enter Artifact, Group, then click on–>Next–>then another new window will be open in that you will provide SpringBoot Version and required dependencies for this project (Spring Data Jpa and MySql Driver ) dependencies are required add these dependencies then click on–>finish.
Then project is created in the editor name as GFG-MAPPING-PROJECT The project Structure looks like as below.
Go to src/main/resources open application.properties in that adding the necessary properties as below
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/schemaname spring.datasource.username=root spring.datasource.password=password spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Go to src/main/java create a package for model classes(ex : com.gfg.model) and create one package for repository (ex :com.gfg.repository). Then create two model classes under the model package
Go to repository package adding the JPA repository of both model classes
Step 7: Go to starter class Autowire two repository interfaces and create objects for model classes
Step 8: Run the Main Application
Right-click on GfgMappingProjectApplication select Run as spring boot application
Then go to Database then check for tables.
select * from address;
select * from employee;
Please Login to comment...