gpt4 book ai didi

java - JSP 文件不使用多对多(Hibernate)从表中检索数据

转载 作者:行者123 更新时间:2023-12-01 12:25:19 24 4
gpt4 key购买 nike

我有两个对象文章和类别。存在多对多的关系。在数据库中,我为此使用中间表 -article_category,如图所示。

/image/Qji5u.jpg

当我在项目中的另一个对象之间使用 OneToMany 关系时,它可以工作。我只是在 ManyToMany 方面遇到麻烦。当我尝试在问题末尾的 jsp 文件中检索相关文章的类别名称时,我想检索相关文章的类别名称。我做错了什么?

这是我的代码:

文章.class

@Entity
public class Article {
@Id
@GeneratedValue
private Integer id;

private String title;
private String content;
private Date publishDate;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "article_category",
joinColumns={@JoinColumn(name="article_id")},
inverseJoinColumns={@JoinColumn(name="category_id")})
private List<Category> categories;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
} }

类别.class

@Entity
public class Category {
@Id
@GeneratedValue
private Integer id;
private String name;

@ManyToMany(mappedBy = "categories")
private List<Article> articles;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}}

IndexController.class

@Controller

公共(public)类 IndexController {

@Autowired
UserService userService;

@Autowired
CategoryService categoryService;

@Autowired
ArticleService articleService;

@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("users", userService.findAll());
model.addAttribute("categories", categoryService.findAll());
model.addAttribute("articles", articleService.findAll());
return "index";
}


@RequestMapping("/article/{id}")
public String ArticleDetail(Model model, @PathVariable Integer id) {
Article article = articleService.findById(id);
model.addAttribute("article1", article);
return "index";
}}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../layouts/taglibs.jsp"%>
<div class="blog-post">
<c:forEach items="${articles}" var="article">
<h2 class="blog-post-title">${article.title}</h2>
<p class="blog-post-meta">${article.publishDate}</p>
<p>${article.content}</p>
</c:forEach>
<p>${article1.user.name}</p>
<p>${article1.user.email}</p>
<p>${article1.category.name}</p> <!-- IT DOES NOT WORK -->
</div>

最佳答案

您必须更改 JSP,因为在多对多关系中,一篇文章具有类别(categories)的集合,而不再是单个类别(category)。您需要一个循环。

更新

类似这样的东西:

<p><c:forEach items="${article1.categories}" var="cat">${cat.name} </c:forEach></p>

关于java - JSP 文件不使用多对多(Hibernate)从表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26399026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com