Spring Data JPA:ManyToMany查询,返回不正确的json

时间:2019-09-03 09:51:31

标签: java spring jpa spring-data-jpa hql

我有2个实体:书籍和作者。每个映射到适当的表。我还有第三张表book_author,因为这两个实体具有多对多关系。 图书:

    public class Book {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int idbook;
        private String bookName;
        private int bookYearWriting;

        @ManyToMany(fetch = FetchType.LAZY,   
            cascade = {
                    CascadeType.PERSIST,        
                    CascadeType.MERGE
            })
        @JoinTable(name = "author_book",
            joinColumns = { @JoinColumn(name = "book_id") },
            inverseJoinColumns  = { @JoinColumn(name = "author_id") }
        )
        private List<Author> authors = new ArrayList<>();
    ...
    }

作者:

    public class Author {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int idauthor;
        private String authorName;

        @ManyToMany(fetch = FetchType.LAZY,  
            cascade = {
                    CascadeType.PERSIST,        
                    CascadeType.MERGE
            },
            mappedBy = "authors")
        private List<Book> books = new ArrayList<>();
    ...
    }

在我的 BookRepository 中,

    // Year + Name + Count
    @Query("Select distinct b from Book b left join b.authors a 
    where b.bookYearWriting = ?1 and 
    (b.bookName like CONCAT('%',?2,'%') or a.authorName like CONCAT('%',?3,'%')) 
    group by b having count(a) = ?4")
    List<Book> findByBookYearWritingIsAndBookNameOrAuthorNameContainingAndAuthorCountIs(int year, String text, String textAgain, long count, Pageable pageable);

参数?2等于?3

我的 BookController

    @GetMapping("/search/{numPage}")
    public List<BookDTO> getAllBooksOnRequest(@PathVariable(value = "numPage") int numPage,@RequestParam(required = false) String text, @RequestParam(required = false) Integer yearWriting, @RequestParam(required = false) Long countAuthors)
    {
        Pageable page = PageRequest.of(numPage, countElementOnPage);
        BookDTO bookDTO = new BookDTO();

        // without param
        if(text == null && yearWriting == null && countAuthors == null) {
            Page<Book> pBook = bookRepository.findAll(page);
            List<Book> lBook = pBook.getContent();
            return bookDTO.getBookDTOList(lBook);
        }
        ...
        else if(text != null && yearWriting != null && countAuthors != null)
        {
            return bookDTO.getBookDTOList(bookRepository.findByBookYearWritingIsAndBookNameOrAuthorNameContainingAndAuthorCountIs(yearWriting, text, text, countAuthors, page));
        }

我执行以下GET请求后 http://localhost:8080/books/search/0?yearWriting=2004&text=1&countAuthors=1

但是我得到了不正确的json

    [
    {
        "bookName": "Book 1",
        "bookYearWriting": 2004,
        "authors": [
            "Author 1"
        ],
        "idBook": 1
    },
    {
        "bookName": "Book 8",
        "bookYearWriting": 2004,
        "authors": [
            "Author 1",
            "Author 3",
            "Author 4"
        ],
        "idBook": 8
    },
    {
        "bookName": "Book 10",
        "bookYearWriting": 2004,
        "authors": [
            "Author 5"
        ],
        "idBook": 10
    }
    ]

书8 不符合countAuthors = 1

为什么会这样?我该如何更改查询以获得正确的结果?

0 个答案:

没有答案