从多对多关系中回归价值

时间:2017-08-16 08:33:27

标签: java hibernate jpa spring-boot thymeleaf

我在SpringBoot中创建一个books数据库应用程序。目前,我正在尝试让我的Controller返回图书表中的所有字段。它与作者表 ManyToMany 关系相关联(第三个表名为 bookauthor )。问题是我得到了这种输出:

Title   Title original  Premiere date   Authors
Harry Potter 1  Harry Potter One    2017-08-02 [eu.fitk.model.Author@70679355, eu.fitk.model.Author@608de73a

我希望返回作者的名称姓氏的值。

这是我的Controller方法的样子:

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ModelAndView oneBook(@PathVariable int id) {
    Book book = bookRepository.findOne(id);
    bookRepository.setCounterForBook(id);
    Publisher publisher = book.getPublisher();
    Set<Author> authors = book.getAuthors();

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("book", book);
    model.put("publisher", publisher);
    model.put("authors", authors);
    return new ModelAndView("books/one", "model", model);
}

这是我的Book(实体模型)类:

@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>();

Thymeleaf模板:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Bookweb</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <!--<link rel="stylesheet" type="text/css"
          href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />-->

    <link th:href="@{/static/bootstrap.css}" rel="stylesheet" />

</head>
<body>
<div th:replace="fragments/header :: header"/>
<div class="container">
    <h2>Pojedyncza książka</h2>
    <table>
        <tr>
            <th>Title</th>
            <th>Title original</th>
            <th>Premiere date</th>
            <th>Authors</th>
        </tr>
        <tr>
            <td th:text="${model.book.title}"></td>
            <td th:text="${model.book.titleOriginal}"></td>
            <td th:text="${model.book.premiereDate}"></td>
            <td th:text="${model.book.authors}"></td>
        </tr>
    </table>
</div>
<script type="text/javascript"
        src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div th:replace="fragments/footer :: footer"/>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

要正确查看作者,只需在Thymeleaf模板中创建foreach循环:

        <h4 th:text="${model.book.titleOriginal}" style="color:slategrey"></h4>
        <a th:each="a: ${model.authors}" th:href="@{/authors/}+${a.authorId}"><h5 th:text="${a.name}+' '+${a.surname}" style="color:#337ab7"></h5></a>
            <small  th:text="${'Views: '+model.book.counter}" style="color:#337ab7"> </small>

        <h4 style="margin-top:30px;">Publisher:</h4>
        <h6 th:text="${model.publisher.name}" style="margin-top:0px;"></h6>