Sunday 20 November 2022

Spring Boot Rest API OneToMany Relationship with JPA Repository

In this Post I am showing how you can easily create a Rest API using JPA Repository with OneToMany Relationships between two Entities.

Project Structure



Calling Response from Postman tool














------------------------------------------ json ------------------------------------------------

{
"title":"Linux",
"author": "Jimmy",
"isbn": "890900",
    "pages" : [{ "number": "30", "content": "Switches", "chapter" : "3"},{ "number": "125", "content":          "Router", "chapter" : "8"}]
}


------------------------------------------ pom.xml -------------------------------------------

# Changing the server port
server.port=9090

# MySQL connection properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=cctns@123
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?createDatabaseIfNotExist=true&useSSL=false

# Log JPA queries
# Comment this in production
spring.jpa.show-sql=true

# Drop and create new tables (create, create-drop, validate, update)
# Only for testing purposes - comment this in production
spring.jpa.hibernate.ddl-auto=update

# Hibernate SQL dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect


---------------------------------------- Book Entity ------------------------------------------------

package com.ramsis.onetomany.entity;

import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "books")
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "title" ,nullable = false)
    private String title;
    @Column(name = "author" ,nullable = false)
    private String author;
    @Column(name = "isbn" ,unique = true)
    private String isbn;
    @JsonIgnoreProperties("book")
    @OneToMany(mappedBy = "book", fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    private Set<Page> pages;

    public Book() {
    }

    public Book(String title, String author, String isbn) {
        this.title = title;
        this.author = author;
        this.isbn = isbn;
    }

    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public Set<Page> getPages() {
        return pages;
    }

    public void setPages(Set<Page> pages) {
        this.pages = pages;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return id.equals(book.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", isbn='" + isbn + '\'' +
                '}';
    }
}

------------------------------------------ Page Entity -----------------------------------------------

package com.ramsis.onetomany.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Table(name = "pages")
public class Page implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
   
    @Column(name = "number", nullable = false)
    private int number;
   
    @Column(name = "content" ,nullable = false)
    private String content;
   
    @Column(name = "chapter", nullable = false)
    private String chapter;
   
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "book_id", nullable = false)
    private Book book;

    public Page() {
    }

    public Page(int number, String content, String chapter, Book book) {
        this.number = number;
        this.content = content;
        this.chapter = chapter;
        this.book = book;
    }

    public Long getId() {
        return id;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getChapter() {
        return chapter;
    }

    public void setChapter(String chapter) {
        this.chapter = chapter;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Page page = (Page) o;
        return id.equals(page.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Page{" +
                "id=" + id +
                ", number=" + number +
                ", content='" + content + '\'' +
                ", chapter='" + chapter + '\'' +
                ", book=" + book +
                '}';
    }
}

----------------------------------------------- BookService -----------------------------------------

package com.ramsis.onetomany.service;

import java.util.List;
import java.util.Set;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.entity.Page;

public interface BookService {

public List<Book> getBooks();
public Book addBook(BookDto book);
public Book updateBook(BookDto book);
public Book getBookByIsbn(String id);

}

-------------------------------------------------BookServiceImpl ----------------------------------

package com.ramsis.onetomany.service;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.dto.PageDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.entity.Page;
import com.ramsis.onetomany.repository.BookRepository;
import com.ramsis.onetomany.repository.PageRepository;

@Service
public class BookServiceImpl implements BookService {

@Autowired
BookRepository bookRepo;

@Autowired
PageRepository pageRepo;

@Override
public Book getBookByIsbn(String id) {
// TODO Auto-generated method stub
return bookRepo.findByIsbn(id);
}

@Override
public Book addBook(BookDto book) {
Book bookEntity = new Book();
Page pageEntity = null;
bookEntity.setAuthor(book.getAuthor());
bookEntity.setIsbn(book.getIsbn());
bookEntity.setTitle(book.getTitle());
Set<PageDto> pageList = book.getPages();
Book bookEntityobj = bookRepo.save(bookEntity);

for (PageDto pagedata : pageList) {
pageEntity = new Page(
pagedata.getNumber(),
pagedata.getContent(),
pagedata.getChapter(),
bookEntityobj);
pageRepo.save(pageEntity);
}

return bookEntityobj;
}

@Override
public Book updateBook(BookDto book) {
Book bookEntity = new Book();
Page pageEntity = null;
bookEntity.setAuthor(book.getAuthor());
bookEntity.setIsbn(book.getIsbn());
bookEntity.setTitle(book.getTitle());
Set<PageDto> pageList = book.getPages();
Book bookEntityobj = bookRepo.save(bookEntity);

for (PageDto pagedata : pageList) {
pageEntity = new Page(
pagedata.getNumber(),
pagedata.getContent(),
pagedata.getChapter(),
bookEntityobj);
pageRepo.save(pageEntity);
}

return bookEntityobj;
}

@Override
public List<Book> getBooks() {
// TODO Auto-generated method stub
return  (List<Book>) bookRepo.findAll();
}

}

---------------------------------------------- Controller ---------------------------------------------

package com.ramsis.onetomany.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.service.BookService;

@RestController
public class PageBookController {
   
@Autowired
BookService bookService;

@GetMapping("/home")
public String home() {
return "Welcome in RestApi with OneToMany Relation, home Page";
}

@PostMapping("/addBook" )
public Book addBook(@RequestBody  BookDto book) {
return this.bookService.addBook(book);
}

@PostMapping("/updateBook" )
public Book updateBook(@RequestBody  BookDto book) {
return this.bookService.updateBook(book);
}

@GetMapping("/getBookIsbn/{eid}")
public Book getBookByIsbn(@PathVariable("eid") String eid) {
return this.bookService.getBookByIsbn(eid);
}

@GetMapping("/getBooks")
public List<Book> getBooks() {
return this.bookService.getBooks();
}
}

------------------------------------------------- BookDto --------------------------------------------

package com.ramsis.onetomany.dto;

import java.util.Set;

public class BookDto {
private Long id;
private String title;
private String author;
private String isbn;
private Set<PageDto> pages;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Set<PageDto> getPages() {
return pages;
}
public void setPages(Set<PageDto> pages) {
this.pages = pages;
}

}

-------------------------------------------------- PageDto -------------------------------------------

package com.ramsis.onetomany.dto;

public class PageDto {
private Long id;
private int number;
private String content;
private String chapter;
private BookDto book;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getChapter() {
return chapter;
}
public void setChapter(String chapter) {
this.chapter = chapter;
}
public BookDto getBook() {
return book;
}
public void setBook(BookDto book) {
this.book = book;
}

}


------------------------------------------------- OnetomanyApplication -------------------------

package com.ramsis.onetomany;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import com.ramsis.onetomany.repository.BookRepository;
import com.ramsis.onetomany.repository.PageRepository;

@SpringBootApplication
@EnableJpaAuditing
public class OnetomanyApplication {

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


@Bean
   public CommandLineRunner mappingDemo(BookRepository bookRepository,
                                        PageRepository pageRepository) {
       return args -> {
        /*
        System.out.print( bookRepository.findById(Long.valueOf(1)));
           // create a new book
           Book book = new Book("Linux", "Cdac", "99844");

           // save the book
           bookRepository.save(book);

           // create and save new pages
           pageRepository.save(new Page(2, "Introduction contents of Linux", "Intro", book));
           pageRepository.save(new Page(102, "Switches", "Switches WAN", book));
           pageRepository.save(new Page(70, "Router", "Router WAN", book));
         
           */
       };
   }
}

-------------------------------------------- BookRepository ---------------------------------------

package com.ramsis.onetomany.repository;

import org.springframework.data.repository.CrudRepository;

import com.ramsis.onetomany.entity.Book;

public interface BookRepository extends CrudRepository<Book, Long> {

    Book findByIsbn(String isbn);
    Book save(Book book);
}

--------------------------------------------------------

No comments:

Post a Comment