通过搜索child/spring boot jpa+postgresql查找父元素

f4t66c6m  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(269)

我正在尝试找出如何找到并返回所有与特定作者姓名相关的书籍。

{
    "bookId": 5,
    "bookName": "test2",
    "publishYear": 2022,
    "publisher": "test2",
    "authors": [
        {
            "id": 5,
            "name": "Heny",
            "surname": "Blakc"
        },
        {
            "id": 6,
            "name": "Garyy",
            "surname": "Harrys"
        }
    ]
}

我想归还所有加里是作者的书。
我使用的是springboot+postgressqlonetomany注解。
如有任何建议,我将不胜感激。
authorcontroller:

package com.example.demo.controller;

import com.example.demo.entity.AuthorEntity;
import com.example.demo.repository.AuthorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/author")
public class AuthorController {
    @Autowired
    AuthorRepository authorRepository;

    @GetMapping("/all")
    public List<AuthorEntity> author(){
        return authorRepository.findAll();
    }
    @PostMapping("/create")
    public AuthorEntity createAuthor(@RequestBody AuthorEntity author){
        AuthorEntity savedAuthor = authorRepository.save(author);
        return savedAuthor;
    }
    @GetMapping("/find/{author}")
    public List<AuthorEntity> findAuthor(@PathVariable(value = "author") String name){
        return authorRepository.findByName(name);
    }

}

图书管理员:

package com.example.demo.controller;

import com.example.demo.entity.BookEntity;
import com.example.demo.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @GetMapping("/all")
    public List<BookEntity> getAllBooks(){
        List<BookEntity> allBookList = bookRepository.findAll();

        return allBookList;
    }

    @GetMapping("/findBook/{name}")
    public List<BookEntity> getBookByName(@PathVariable(value = "name")String bookName)
    {
        return bookRepository.findByBookName(bookName);
    }

    @GetMapping("/year/{year}")
    public List<BookEntity> getBookByYear(@PathVariable(value = "year")Integer year)
    {
        return bookRepository.findByPublishYear(year);
    }
    @GetMapping("/publisher/{publisher}")
    public List<BookEntity> getBookByPublisher(@PathVariable(value = "publisher")String publisher)
    {
        return bookRepository.findByPublisher(publisher);
    }

    @PostMapping("/create-book")
    public BookEntity createBook (@RequestBody BookEntity book){
        BookEntity savedBook = bookRepository.save(book);

        return savedBook;
    }

}

作者单位:

package com.example.demo.entity;

import javax.persistence.*;

@Entity
@Table(name = "authors")
public class AuthorEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "name", nullable = false)
    private String name;
    @Column(name = "surname",nullable = false)
    private String surname;

    @ManyToOne(fetch =FetchType.LAZY)
    private BookEntity books;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @Override
    public String toString() {
        return "AuthorEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", surname='" + surname + '\'' +
                ", books=" + books +
                '}';
    }

}

账簿主体:

package com.example.demo.entity;

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

@Entity
@Table(name = "books")
public class BookEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Integer bookId;

    @Column(name = "book_name", nullable = false)
    private String bookName;

    @Column(name = "publish_year",nullable = false)
    private Integer publishYear;

    @Column(name = "publisher",nullable = false)
    private String publisher;

    @OneToMany(targetEntity = AuthorEntity.class, cascade = CascadeType.ALL)
    private List<AuthorEntity> authors;

    public List<AuthorEntity> getAuthors() {
        return authors;
    }

    public void setAuthors(List<AuthorEntity> authors) {
        this.authors = authors;
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Integer getPublishYear() {
        return publishYear;
    }

    public void setPublishYear(Integer publishYear) {
        this.publishYear = publishYear;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    @Override
    public String toString() {
        return "BookEntity{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", publishYear=" + publishYear +
                ", publisher='" + publisher + '\'' +
                ", authors=" + authors +
                '}';
    }
}

作者地址:

package com.example.demo.repository;

import com.example.demo.entity.AuthorEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface AuthorRepository extends JpaRepository<AuthorEntity, Integer> {
    List<AuthorEntity> findByName(String name);
}

图书存储库:

package com.example.demo.repository;

import com.example.demo.entity.BookEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookRepository extends JpaRepository<BookEntity, Integer> {
    public List<BookEntity> findByBookName(String bookName);
    public List<BookEntity> findByPublishYear(Integer year);
    public List<BookEntity> findByPublisher(String publisher);
}
yqhsw0fo

yqhsw0fo1#

有多种方法可以做到这一点,一种方法是使用 forEach and filter 像java一样

List<BookeEntity> books = getBooks();
 books.forEach(e->{
        List<AuthorEntity> al= e.getAuthors().stream().filter(ee->ee.getName().equals("Garyy")).collect(Collectors.toList());
        e.setAuthors(al);
    });
cu6pst1q

cu6pst1q2#

你的数据库设计错了。现实:
一位作者写了一本或多本书,
一个或多个作者写的一本书。
因此,您需要一本表作者手册来将关系many many转换为2个关系:one many,many one。
你可以看到https://www.stat.auckland.ac.nz/~paul/itdt/html/node42.html 第5.6.3.2节关系

相关问题