Java 这本书的作者名单

Java 这本书的作者名单,java,hibernate,spring-boot,thymeleaf,Java,Hibernate,Spring Boot,Thymeleaf,我被一个问题困住了。我想将数据库中的所有书籍显示为一个列表,更重要的是,每一本都会显示其作者的列表。以下是我的控制器方法的外观: @RequestMapping(value="/new",method = RequestMethod.GET) public ModelAndView newBooks() { List<Book> books = bookRepository.findAllByOrderByPremiereDateDesc();

我被一个问题困住了。我想将数据库中的所有书籍显示为一个列表,更重要的是,每一本都会显示其作者的列表。以下是我的控制器方法的外观:

@RequestMapping(value="/new",method = RequestMethod.GET)
    public ModelAndView newBooks() {
        List<Book> books = bookRepository.findAllByOrderByPremiereDateDesc();
        Set<Author> authors = new HashSet<>();
        for(Book b:books){
            authors = b.getAuthors();

        }

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("books",books);
        model.put("authors", authors);

        return new ModelAndView("books/new","model",model);
    }
@RequestMapping(value=“/new”,method=RequestMethod.GET)
公共模型和视图新图书(){
List books=bookRepository.findAllByOrderByPremiereDateDesc();
Set authors=new HashSet();
(b册:书籍){
authors=b.getAuthors();
}
映射模型=新的HashMap();
模型。放置(“书籍”,书籍);
模型。put(“作者”,作者);
返回新模型和视图(“书籍/新”、“模型”、“模型”);
}
但我不知道如何将书籍与其作者联系起来,以便在我看来正确地展示它们:

<table>
        <tr>
            <th>Title</th>
            <th>Title original</th>
            <th>Premiere date</th>
            <th>Authors</th>
        </tr>
        <tr th:each="b : ${model.books}">
            <td th:text="${b.title}"></td>
            <td th:text="${b.titleOriginal}"></td>
            <td th:text="${b.premiereDate}"></td>
        <tr th:each="a: ${b.authors}">
            <td th:text="${a.name}"></td>
            <td th:text="${a.surname}"></td>
        </tr>

    </table>

标题
标题原件
首映日期
作者
还有我的图书(实体)模型类

@Entity
@Table(name = "book")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "book_id")
    private int bookId;
    @Column(name = "isbn")
    private long isbn;
    @Column(name = "title")
    private String title;
    @Column(name = "title_original")
    private String titleOriginal;
    @Column(name = "premiere_date")
    private Date premiereDate;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "book_author", joinColumns = {@JoinColumn(name = "book_id")}, inverseJoinColumns = {@JoinColumn(name = "author_id")})
    private Set<Author> authors = new HashSet<Author>();
@实体
@表(name=“book”)
公共课堂用书{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@列(name=“book\u id”)
私人书号;
@列(name=“isbn”)
私人长期isbn;
@列(name=“title”)
私有字符串标题;
@列(name=“title\u原件”)
私有字符串标题原件;
@列(name=“首映日”)
私人约会首映;
@多个(级联=级联类型.ALL)
@JoinTable(name=“book\u author”,joinColumns={@JoinColumn(name=“book\u id”)},inverseJoinColumns={@JoinColumn(name=“author\u id”)})
private Set authors=new HashSet();

尝试嵌套循环!



尝试嵌套循环!

如果我想让它看起来像:Book1:author1、author2、author3等;Book2:author1、author3等?Nvm,明白了!谢谢你的帮助,伙计:)如果我想让它看起来像:Book1:author1、author2、author3等怎么办;Book2:author1、author3等?Nvm,明白了!谢谢你的帮助,伙计:)