0

我得到 java.lang.IllegalArgumentException:当我尝试对我的表进行更新时,修改查询只能使用 void 或 int/Integer 作为返回类型。

1. thymeleaf

 <div class="form-popup" id="myForm">
 <form  action=""th:action="@{/searchreader(reader_id=${givebook?.getReader()?.getI dNumber()}, book_id=${givebook?.getId()})}"  th:object="${givebook}" class="form-container">

            <label for="book_id"><b>Book ID</b></label>
            <input type="text" name="id" th:value="${reader?.idNumber}" placeholder="Reader ID" hidden/>
            <input type="text"  th:value="${givebook?.getId()}" placeholder="Enter Book ID" name="book_id" required>

            <button type="submit" class="btn">OK</button>
            <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
        </form>
    </div>

2.Controller

 @GetMapping({"/searchreader","/searchreader{id}","/searchreader{id}/{book_id}"})
    public String searchReaderById(@RequestParam("id") Optional<Long> id, @RequestParam("book_id") Optional<Long> book_id, Model model){

        if(id.isPresent()&&book_id.isPresent()){
            model.addAttribute("givebook",bookRepository.updateBookSetReaderId(id.get(),book_id.get()));
            return "reader/searchreader";
        }
        else
        if(id.isPresent()) {
            Reader reader = readerService.findReaderByIdNumber(id.get());
            model.addAttribute("reader", reader);
            model.addAttribute("messages",readerService.getMessages());

            return "reader/searchreader";
        } else
        {
            return "reader/searchreader";}
        }

3.Repository

    @Transactional
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Query(value = "update Book set reader_id=(select r.id from reader r where r.id_Number=:reader_id) where id=:book_id",nativeQuery = true)
    Book updateBookSetReaderId(Long reader_id,Long book_id);

虽然更新的语句返回修改后的行,但存储库中的方法应该返回 void 或 integer,但是当我修改返回类型 void 或 integer 时,我的控制器不起作用,我的百里香模板也不起作用,而我使用类中的方法书。

请任何想法。谢谢。

4

1 回答 1

0

在存储库中

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(value = "update Book set reader_id=(select r.id from reader r where r.id_Number=:reader_id) where id=:book_id",nativeQuery = true)
Integer updateBookSetReaderId(Long reader_id,Long book_id);

Book findBookByBookId(Long bookId);

在控制器中

    if(id.isPresent()&&book_id.isPresent()){
        Book getBook = null;
        Integer updatedRow = bookRepository.updateBookSetReaderId(id.get(),book_id.get());
        if(updatedRow > 0){
        getBook = bookRepository.findBookByBookId(book_id.get());
        }
        model.addAttribute("givebook",getBook);
        return "reader/searchreader";
    }
于 2019-11-03T00:13:13.617 回答