Many-To-Many Relationship in JPA

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.

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.

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=

You may also like...

Leave a Reply

Your email address will not be published.