Many-To-Many Relationship in JPA

JPA Many to Many example with Hibernate in Spring Boot

A relationship is a connection between two types of entities. In the case of a many-to-many relationship, both sides can relate to multiple instances of the other side. Many-To-Many Relationship in JPA

Note that it’s possible for entity types to be in a relationship with themselves. Think about the example of modeling family trees: Every node is a person, so if we talk about the parent-child relationship, both participants will be a person.Many-To-Many Relationship in JPA

JPA Many to Many example with Hibernate in Spring Boot

However, it doesn’t make such a difference whether we talk about a relationship between single or multiple entity types. Since it’s easier to think about relationships between two different entity types, we’ll use that to illustrate our cases.

Post Class Entity 

package com.example.movieslist.Post;

import com.example.movieslist.Tag.TagList;
import org.aspectj.apache.bcel.generic.Tag;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int id;
    String title;
    String decription;
    String Content;
    Date date;
    @ManyToMany(cascade = CascadeType.ALL)
    List<TagList> tagList;



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDecription() {
        return decription;
    }

    public void setDecription(String decription) {
        this.decription = decription;
    }

    public String getContent() {
        return Content;
    }

    public void setContent(String content) {
        Content = content;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public List<TagList> getTagList() {
        return tagList;
    }

    public void setTagList(List<TagList> tagList) {
        this.tagList = tagList;
    }
}

Tag Class Entity

package com.example.movieslist.Tag;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class TagList {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int id;
    String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Post Controller
package com.example.movieslist.Api;

import com.example.movieslist.Post.Post;
import com.example.movieslist.Post.PostRespository;
import com.example.movieslist.Tag.TagList;
import com.example.movieslist.Tag.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class ControllerPost {

    @Autowired
    PostRespository postRespository;
    @Autowired
    TagRepository tagRepository;


    @PostMapping("/add/post")
    public Post addPost(Post post){

        List<TagList> tagList=new ArrayList<>();

        TagList tag=new TagList();
        tag.setName("how to make a game in hinid");
        TagList tag1=new TagList();
        tag1.setName("My name is Haider ");
        tagList.add(tag);
        tagList.add(tag1);

        post.setTitle("How to Earn Money ");
        post.setDate(new Date());
        post.setDecription("how to make a money online in hindi");
        post.setContent("In this is we will be learn how to make a game in hindi");
        post.setTagList(tagList);
        return postRespository.save(post);
    }

    @PutMapping("/update/post")
    public Post updatedata(Post post,String id){
        post =postRespository.findPostById((int) Long.parseLong(id));
        post.setTitle("Learning thind");
        post.setContent("how to learn id");
        post.setDecription("learing new thing");
        post.setDate(new Date());
        return postRespository.save(post);
    }


    @DeleteMapping("/delete/post")
    public void deletepost(Integer id)
    {
        postRespository.deleteById(id);
    }

}

Main Class 

package com.example.movieslist;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MoviesListApplication {

    public static void main(String[] args) {
        SpringApplication.run(MoviesListApplication.class, args);
    }

}


Application Properties
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/haider
spring.datasource.username=root
spring.datasource.password=

Sure, here are the application properties that you need to set to connect to a MySQL database:

  • spring.jpa.database-platform: The name of the database platform. In this case, it is MySQL.
  • spring.jpa.hibernate.ddl-auto: The strategy for creating or updating the database schema. In this case, it is update, which means that Spring Boot will update the schema to match the entities in your application.
  • spring.datasource.driverClassName: The fully qualified name of the JDBC driver for MySQL.
  • spring.datasource.url: The JDBC URL for connecting to the MySQL database.
  • spring.datasource.username: The username for connecting to the MySQL database.
  • spring.datasource.password: The password for connecting to the MySQL database.

Once you have set these properties, you can start your Spring Boot application and it will connect to the MySQL database.

Here are some additional tips for connecting to a MySQL database:

  • If you are using a different database platform, you will need to change the spring.jpa.database-platform property to the name of the platform that you are using.
  • If you are using a different version of MySQL, you may need to change the spring.datasource.driverClassName property to the fully qualified name of the JDBC driver for the version of MySQL that you are using.
  • If you are using a different username or password for connecting to the MySQL database, you will need to change the spring.datasource.username and spring.datasource.password properties to the username and password that you are using.

Leave a Reply

Your email address will not be published. Required fields are marked *