<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Akashdeep</title>
    <description>The latest articles on DEV Community by Akashdeep (@akashdeep).</description>
    <link>https://dev.to/akashdeep</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1921487%2Fced2d18c-0a03-404e-af98-1b9bed6f50d0.png</url>
      <title>DEV Community: Akashdeep</title>
      <link>https://dev.to/akashdeep</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akashdeep"/>
    <language>en</language>
    <item>
      <title>Load Balancer Algorithms</title>
      <dc:creator>Akashdeep</dc:creator>
      <pubDate>Tue, 21 Jan 2025 08:58:03 +0000</pubDate>
      <link>https://dev.to/akashdeep/load-balancer-algorithms-3bfi</link>
      <guid>https://dev.to/akashdeep/load-balancer-algorithms-3bfi</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction: What is a Load Balancer?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the realm of distributed systems, a load balancer acts as a traffic manager that distributes network or application traffic across multiple servers. Its primary goal is to ensure no single server bears an excessive load while optimizing resource utilization, minimizing response times, and maintaining system reliability.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem Without a Load Balancer and a Single Server Setup&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A system with only one server is a single point of failure. If this server becomes overwhelmed or crashes, the entire application goes offline. Additionally, such setups struggle to handle increasing traffic, leading to slower response times and reduced user satisfaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem with Horizontal Scaling Without a Load Balancer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Horizontal scaling involves adding more servers to handle increasing traffic. However, without a load balancer, there is no mechanism to efficiently distribute requests among servers. This can lead to uneven loads, with some servers becoming overutilized while others remain underutilized. Consequently, the system cannot take full advantage of the additional servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Are Health Checks Important?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Health checks ensure the load balancer routes traffic only to healthy and responsive servers. By regularly monitoring server status, health checks prevent traffic from being sent to failed or underperforming servers, thereby improving reliability and user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Load Balancer Algorithms&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Load balancers use algorithms to decide how to distribute traffic among servers. These algorithms fall into two main categories: static and dynamic.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Static Algorithms&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Static algorithms use predefined rules for traffic distribution. They are simple and fast but lack adaptability to changing server conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Characteristics of Static Algorithms:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Predefined Rules:&lt;/strong&gt; Traffic distribution follows fixed patterns or rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple and Fast:&lt;/strong&gt; Minimal computational overhead makes them efficient.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not Adaptive:&lt;/strong&gt; They do not consider real-time server health or load.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples of Static Algorithms:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Round Robin:&lt;/strong&gt; Requests are distributed sequentially to each server in the pool. Once the last server is reached, the algorithm loops back to the first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IP Hashing:&lt;/strong&gt; The client’s IP address is hashed to determine the server for routing, ensuring consistent server assignments for returning clients.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;URL Hashing:&lt;/strong&gt; Requests are routed based on a hash of the requested URL, which is useful for caching and content distribution.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Dynamic Algorithms&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Dynamic algorithms make decisions based on real-time metrics like server health, load, and response time. They are more accurate but require continuous monitoring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Characteristics of Dynamic Algorithms:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-Time Metrics&lt;/strong&gt;: Decisions are informed by live server data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accurate:&lt;/strong&gt; Better distribution leads to optimized performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requires Monitoring:&lt;/strong&gt; Relies on constant feedback from servers, increasing complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples of Dynamic Algorithms:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Least Connections:&lt;/strong&gt; Routes traffic to the server with the fewest active connections. This is ideal for long-lived connections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Least Response Time:&lt;/strong&gt; Routes traffic to the server with the quickest response time, ensuring faster user experiences.&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>algorithms</category>
      <category>loadbalancer</category>
    </item>
    <item>
      <title>Big O Complexity for Graphs: Adjacency Matrix vs Adjacency List</title>
      <dc:creator>Akashdeep</dc:creator>
      <pubDate>Tue, 14 Jan 2025 17:56:58 +0000</pubDate>
      <link>https://dev.to/akashdeep/big-o-complexity-for-graphs-adjacency-matrix-vs-adjacency-list-3akf</link>
      <guid>https://dev.to/akashdeep/big-o-complexity-for-graphs-adjacency-matrix-vs-adjacency-list-3akf</guid>
      <description>&lt;p&gt;Graphs can be represented in two main ways: Adjacency Matrix and Adjacency List. Each method has its own pros and cons depending on how much memory it needs and how quickly it can handle different operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Space Complexity
&lt;/h2&gt;

&lt;p&gt;Adjacency Matrix                          : O(V^2)&lt;br&gt;
Adjacency List                            : O(V+E)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;V: Number of vertices.&lt;/li&gt;
&lt;li&gt;E: Number of edges.&lt;/li&gt;
&lt;li&gt;Adjacency Matrix requires a V×V grid, making it less efficient for sparse graphs.&lt;/li&gt;
&lt;li&gt;Adjacency List only stores edges for each vertex, making it space-efficient for sparse graphs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Time Complexity: Adding a Vertex
&lt;/h2&gt;

&lt;p&gt;Adjacency Matrix                          : O(V^2)&lt;br&gt;
Adjacency List                            : O(1)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adjacency Matrix: Adding a vertex may require creating a new 𝑉+1×𝑉+1 matrix and copying the old matrix, which is expensive.&lt;/li&gt;
&lt;li&gt;Adjacency List: Simply add a new empty list for the new vertex, which is constant time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Time Complexity: Adding an Edge
&lt;/h2&gt;

&lt;p&gt;Adjacency Matrix                          : O(1)&lt;br&gt;
Adjacency List                            : O(1)&lt;/p&gt;

&lt;p&gt;Both representations allow constant-time edge additions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Matrix: Set the matrix cell to 1 (or edge weight).&lt;/li&gt;
&lt;li&gt; List: Append the edge to the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Time Complexity: Removing an Edge
&lt;/h2&gt;

&lt;p&gt;Adjacency Matrix                          : O(1)&lt;br&gt;
Adjacency List                            : O(E/V)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adjacency Matrix: Directly unset the cell, which is constant time.&lt;/li&gt;
&lt;li&gt;Adjacency List: Requires searching through the list for the edge, leading to O(E/V) time on average.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Time Complexity: Removing a Vertex
&lt;/h2&gt;

&lt;p&gt;Adjacency Matrix                          : O(V^2)&lt;br&gt;
Adjacency List                            : O(V+E)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adjacency Matrix: Removing a vertex involves deleting its row and column, which can require shifting elements in the matrix.&lt;/li&gt;
&lt;li&gt;Adjacency List: Removing a vertex requires deleting the list for the vertex and traversing all other lists to remove edges to the vertex.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Choosing the Right Representation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Adjacency Matrix:&lt;br&gt;
Suitable for dense graphs where E≈V2.&lt;br&gt;
Offers constant-time edge operations but consumes more space.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adjacency List:&lt;br&gt;
Ideal for sparse graphs where E≪V2.&lt;br&gt;
Space-efficient and generally faster for operations involving vertices.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>graphrepresentation</category>
      <category>dsa</category>
      <category>adjacencymatrix</category>
      <category>adjacencylist</category>
    </item>
    <item>
      <title>Hosting a Custom Login and Registration UI with AWS Amplify and AWS Cognito</title>
      <dc:creator>Akashdeep</dc:creator>
      <pubDate>Tue, 13 Aug 2024 10:34:03 +0000</pubDate>
      <link>https://dev.to/akashdeep/hosting-a-custom-login-and-registration-ui-with-aws-amplify-and-aws-cognito-g72</link>
      <guid>https://dev.to/akashdeep/hosting-a-custom-login-and-registration-ui-with-aws-amplify-and-aws-cognito-g72</guid>
      <description>&lt;p&gt;Walk through the steps to adopt a custom login and registration user interface (UI) using AWS Amplify and AWS Cognito. AWS Amplify is a powerful tool that provides simplified framework for developing and running cloud-powered applications, while AWS Cognito provides secure authentication with user management. By the end of this guide, you will have a custom login and registration UI implemented and connected to AWS Cognito for user authentication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Guide&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Begin by installing the AWS Amplify CLI globally on your system. This will enable you to interact with Amplify services from the command line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g @aws-amplify/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Configure AWS Amplify&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Set up the Amplify CLI with your AWS credentials. Follow the prompts to configure your AWS account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amplify configure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Initialize Amplify in Your Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Initialize an Amplify project in your React application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amplify init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add Authentication with AWS Cognito&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add authentication to your Amplify project using AWS Cognito.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amplify add auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deploy Changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Push your changes to the cloud. This will configure AWS Cognito and other backend resources.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amplify push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Install Required Packages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install AWS Amplify and the Amplify UI components for React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install aws-amplify @aws-amplify/ui-react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Start Your React Application&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run your React application locally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Initialize a Git Repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Initialize a Git repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Commit Your Changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stage and commit your changes with an initial commit message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "Initial commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Set Up Git Branch&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git branch -M main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Connect to Remote Repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add a remote repository URL to your Git configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add origin &amp;lt;repository URL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Push Changes to Remote Repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Push your commits to the remote repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push -u origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Access AWS Amplify Console&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Log in to the AWS Management Console.&lt;/li&gt;
&lt;li&gt;Navigate to the AWS Amplify Console.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Connect Your Repository&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the Amplify Console, click on "Get Started" under the "Deploy" section.&lt;/li&gt;
&lt;li&gt;Choose your repository provider (e.g., GitHub, GitLab, Bitbucket, AWS CodeCommit).&lt;/li&gt;
&lt;li&gt;Authenticate and authorize AWS Amplify to access your repository.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Configure the Build Settings&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose the repository you want to connect.&lt;/li&gt;
&lt;li&gt;Select the branch you want to deploy (usually master).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Save and Deploy&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click "Save and Deploy". AWS Amplify will start the deployment process, building and deploying your application based on the configuration provided.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By integrating AWS Amplify with your CI/CD pipeline, you automate the process of building, testing, and deploying your React application. This setup streamlines your development workflow and ensures that changes are quickly and reliably reflected in your live environment.&lt;/p&gt;

&lt;p&gt;If you need further customization or have specific requirements, refer the AWS Amplify documentation for more detailed information on advanced CI/CD configurations and best practices.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Diagram showing the CI/CD workflow.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F107dbshp6yldf3i9p86x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F107dbshp6yldf3i9p86x.png" alt="Image description" width="800" height="888"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cognito</category>
      <category>devops</category>
      <category>reactnative</category>
      <category>awschallenge</category>
    </item>
    <item>
      <title>Understanding JPA Mappings in Spring Boot: One-to-One, One-to-Many, Many-to-One, and Many-to-Many Relationships</title>
      <dc:creator>Akashdeep</dc:creator>
      <pubDate>Tue, 13 Aug 2024 07:43:10 +0000</pubDate>
      <link>https://dev.to/akashdeep/understanding-jpa-mappings-in-spring-boot-one-to-one-one-to-many-many-to-one-and-many-to-many-relationships-1m07</link>
      <guid>https://dev.to/akashdeep/understanding-jpa-mappings-in-spring-boot-one-to-one-one-to-many-many-to-one-and-many-to-many-relationships-1m07</guid>
      <description>&lt;p&gt;When building applications with Spring Boot and JPA (Java Persistence API), managing relationships between entities is crucial. Understanding how to map these relationships effectively will help you model your data accurately and perform efficient queries. In this guide, we'll explore the different types of JPA mappings supported by Spring Boot: one-to-one, one-to-many, many-to-one, and many-to-many. Will provide example on both unidirectional and bidirectional mappings.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;One-to-One Relationship&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Unidirectional One-to-One
&lt;/h2&gt;

&lt;p&gt;In a unidirectional one-to-one relationship, one entity references another without the second entity knowing about the first.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// User.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Data
@Entity
public class User {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String username;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_profile_id", referencedColumnName = "id")
    private Profile profile;

}

// Profile.java
import javax.persistence.Entity;
import javax.persistence.Id;

@Data
@Entity
public class Profile {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String bio;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bidirectional One-to-One
&lt;/h2&gt;

&lt;p&gt;In a bidirectional one-to-one relationship, both entities reference to each other.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// User.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Data
@Entity
public class User {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String username;

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
    private Profile profile;

}

// Profile.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.JoinColumn;

@Data
@Entity
public class Profile {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String bio;

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;One-to-Many Relationship&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Unidirectional One-to-Many
&lt;/h2&gt;

&lt;p&gt;In a unidirectional one-to-many relationship, one entity references a collection of another entity.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Author.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.List;

@Entity
@Data
public class Author {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_book_id", referencedColumnName = "id")
    private List&amp;lt;Book&amp;gt; books;
}

// Book.java
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
@Data
public class Book {
    @Id
    private Long id;
    private String title;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bidirectional One-to-Many
&lt;/h2&gt;

&lt;p&gt;In a bidirectional one-to-many relationship, both entities are referenced to each other.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Author.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import java.util.List;

@Entity
@Data
public class Author {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String name;

    @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List&amp;lt;Book&amp;gt; books;

}

// Book.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
@Data
public class Book {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String title;

    @ManyToOne(name = "fk_author_id", referencedColumnName = "id")
    private Author author;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Many-to-One Relationship&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Unidirectional Many-to-One
&lt;/h2&gt;

&lt;p&gt;In a unidirectional many-to-one relationship, multiple entities reference a single entity.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Order.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
@Data
public class Order {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String description;

    @ManyToOne
    private Customer customer;

}

// Customer.java
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
@Data
public class Customer {

    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String name;

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bidirectional Many-to-One
&lt;/h2&gt;

&lt;p&gt;In a bidirectional many-to-one relationship, the referenced entity has a collection of the referring entities.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Order.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
@Data
public class Order {

    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String description;

    @ManyToOne
    private Customer customer;

}

// Customer.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.List;

@Entity
@Data
public class Customer {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String name;

    @OneToMany(mappedBy = "customer")
    private List&amp;lt;Order&amp;gt; orders;

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Many-to-Many Relationship&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Unidirectional Many-to-Many
&lt;/h2&gt;

&lt;p&gt;In a unidirectional many-to-many relationship, each entity has a collection of the other, but the relationship is not explicitly managed by either side.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Set;

@Entity
@Data
public class Student {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String name;

    @ManyToMany
    private Set&amp;lt;Course&amp;gt; courses;

}

// Course.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Set;

@Entity
@Data
public class Course {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String title;

    @ManyToMany
    private Set&amp;lt;Student&amp;gt; students;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bidirectional Many-to-Many
&lt;/h2&gt;

&lt;p&gt;In a bidirectional many-to-many relationship, both entities are referencing each other, and the relationship is explicitly managed.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Set;

@Entity
@Data
public class Student {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String name;

    @ManyToMany(mappedBy = "students")
    private Set&amp;lt;Course&amp;gt; courses;
}

// Course.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Set;

@Entity
@Data
public class Course {
    @Id
    @GeneratedValue(stratergy = GenerationType.Identity)
    private Long id;
    private String title;

    @ManyToMany
    private Set&amp;lt;Student&amp;gt; students;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>springboot</category>
      <category>jpa</category>
      <category>hibernate</category>
      <category>java</category>
    </item>
  </channel>
</rss>
