Wednesday, February 02, 2022

One-To-One Relationship In Spring Boot Between The Entities

springboot,java,hibernate,jpa,onetoone,programming,software development,technology
In Spring Boot, a One-To-One relationship is one in which both entities are associated with each other. In this tutorial, we will learn how to implement a One-To-One relationship between entities in Spring Boot using JPA and Hibernate.


1. What is a One-To-One association

Before delving into the specifics of creating relationships between entities in Hibernate, consider how we create relationships between objects in JAVA. An object association in JAVA is very simple: we use an attribute (variable) of a class to do so. The definitions of two simple classes, Factory and Product, are provided below:
// Factory class
  public class Factory {
    …
    // Factory can make product
    private Product product;
    …
  }
// Product class
  public class Product {
    …
    private String productName;
    …
  }
We've declared a Factory POJO with a Product class attribute variable (product). In JAVA, we can create a relationship (association) between the Factory and Product classes using attributes.
JAVA,Hibernate,Autowired,PostMapping,unidirectional,Spring Boot,RestController,JpaRepository,OneToOne Mapping,Object-relational mapping (ORM),RequestMapping,


A One-To-One relationship is when one JAVA object is linked to another. A motorcycle, for example, has an engine, and each engine belongs to a specific motorcycle; similarly, any location on Earth must have a geolocation, and the geolocation refers to a specific location.

Consider for a moment how we can store the objects in a relational database. Because traditional databases store data in two-dimensional table format (rows and columns), relationships between tables are maintained using the primary key and foreign key. However, objects store data in variables, and we cannot store object associations directly in database tables using our traditional JDBC APIs.

We can solve this problem with the help of an object-relational mapping (ORM) tool. Hibernate is one of the most widely used ORM tools. We can use Hibernate to automate the process of saving the association of JAVA objects to database tables, as well as accessing database table data and converting it back into JAVA objects. In Spring Boot, we use some special annotations to convert our plain JAVA class into a persistent class or entity, and then use Hibernate to access and manipulate the data. Hibernate has implemented JPA (JAVA Persistence API) and uses it for data manipulation to interact with relational databases. JAVA defines JPA as an object-relational mapping specification. Hibernate provides us with a collection of classes and methods for data persistence with this specification.

Now it is time to make our hands dirty. Let’s dive into coding.💪

2. Spring Boot project creation

We will use Spring Initializr to create a new Spring Boot project, which will generate a basic structure for our Spring Boot project. The following dependencies have been added:
  • Spring Boot DevTools - necessary development tools  
  • Spring Web - for Spring MVC and embedded Tomcat that will run the Spring Boot application  
  • Spring Data JPA - Java Persistence API
  • MySQL Driver - JDBC driver for MySQL (for other DB you have to choose that dependency for that DB)
JAVA,Hibernate,Autowired,PostMapping,unidirectional,Spring Boot,RestController,JpaRepository,OneToOne Mapping,Object-relational mapping (ORM),RequestMapping,
Then, to download the project zip file, click GENERATE. Unzip the zip archive. Import the project as a Maven project into Eclipse/STS.

3. Connect to the Database

We'll put the connection information in the application because we're using MySQL as our database. Hibernate will use this information to connect to the database as the application.properties file has a name/value pair. The connection information is described in the following snippet:
JAVA,Hibernate,Autowired,PostMapping,unidirectional,Spring Boot,RestController,JpaRepository,OneToOne Mapping,Object-relational mapping (ORM),RequestMapping,
Here, we've set datasource.url to the URL of our JDBC connection. The database credentials are mentioned in datasource.user and datasource.password.

Spring Boot can gather the necessary information about the database from the connection URL, so it is not necessary to specify datasource.driver-class-name. However, we will be safer if we specify the driver-class-name.

When the application is running, jpa.show-sql displays Hibernate SQL queries in the console, jpa.hibernate.ddl-auto is set to update, which updates the database schema every time we restart the application, and hibernate.dialect indicates which database dialect we are using.


👉 To demonstrate, we'll make two entities: the Motorcycle and the Engine. Then, establish a One-To-One relationship between these two entities.
...
...

You can download the source code.
Happy coding!!! ðŸ˜Š
in

1 comment:

Popular posts