我有两个实体:书籍和作者。这是多对多的关系。我想获取包含所有书籍列及其作者的表格,并在春季应用程序中使用 Thymeleaf 将它们展示到表格中。
这里 预订课程:
@Entity
@Table(name="book", schema="booksloan")
public class Book{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String title;
private String year;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},mappedBy = "book")
private Set<Author> author = new HashSet<>();
public Book() {
}
// getters and setters
}
在这里 作者类:
@Entity
@Table(name="Author", schema="booksloan")
public class Author{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id_author;
private String name;
private String surname;
@ManyToMany
@JoinTable(
name = "written",
joinColumns = @JoinColumn(name = "id_author"),
inverseJoinColumns = @JoinColumn(name = "id"))
private Set<Book> books = new HashSet<>();
public Author() {
}
// getters and setters
}
我不知道这个书面类是否有用:
@Entity
public class Written{
private int id;
private int id_author;
public Written() {
}
// getters and setters
}
这里书库界面
@Repository
public interface BookRepository extends JpaRepository<Libro, Integer> {
@Query(value = "SELECT * FROM Written s JOIN Book l on s.id = l.id JOIN Author a on a.id_author = s.id_author", nativeQuery = true)
public List<Book> findWrittenBy();
}
在应用控制器类中
@RequestMapping(value={"", "/", "/index"})
public String index(Model model) {
List<Book> listBooksTest = bookService.findWrittenBy();
model.addAttribute("listBooksTest", listBooksTest);
return "indexUser";
}
}
这里索引用户
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>Books</title>
</head>
<body>
<div align="center">
<h1>Books list</h1>
<br />
<table border="1" cellpadding="10">
<thead>
<tr>
<th>ID Book</th>
<th>ID Author</th>
<th>Title</th>
<th>Year</th>
<th>Name Author</th>
<th>Surname Author</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${listBooksTest}">
<td th:text="${book.id}">ID Book</td>
<td th:text="${book.id_author}">ID Author</td>
<td th:text="${book.title}">Title</td>
<td th:text="${book.year}">Year</td>
<td th:text="${book.name}">Name Author</td>
<td th:text="${book.surname}">Surname Author</td>
<td><a th:href="@{'/copy/' + ${book.id}}">View copy</a>
</td>
</tr>
</tbody>
</table>
<br />
</div>
</body>
</html>
这是我得到的错误
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'id_author' cannot be found on object of type 'net.assignment.booksLoan.model.Book' - maybe not public or not valid?
有人知道我该怎么做吗?
您知道,与其浪费时间(我们讨厌浪费时间,不是吗(弄清楚哪个特定查询适合实体的哪一侧,您可以将结果列组合成自定义模型/类。或者以类似的方式,只需将来自List<Book>
和List<Author>
的相交行组合在同一控制器中并将它们传递给视图。
理想情况下,您的查询将是
@Query(value = "w.id, w.id_author, b.id, b.title, b.year, a.id_author, a.name, a.surname
FROM Book b
INNER JOIN
Written w on b.id = w.id
INNER JOIN
Author a on w.id_author = a.id_author ", nativeQuery = true)
public List <IntersectionResult> findAllIntersectedColumn ();
问题是您的列表的签名是 Book,并且您还希望获得name
和surename
但它们在Book中没有明确定义为字段,即使它们通过 M到 M模型相互关联。还有一个关于投影接口的主题,用于检索相关属性的子集,您可以在 Spring 的文档上阅读。