JSP文件不从具有多对多(Hibernate)的表中检索数据

时间:2014-10-16 07:53:07

标签: java hibernate jsp

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

http://i.stack.imgur.com/Qji5u.jpg

当我在项目中的其他对象之间使用OneToMany关系时,它可以工作。我和ManyToMany一起遇到麻烦。当我在guestion结束时尝试在jsp文件中时,我想检索相关文章的类别名称。我做错了什么?

有我的代码:

Article.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;
}   }

Category.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 class 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>

2 个答案:

答案 0 :(得分:1)

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

更新

这样的事情:

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

答案 1 :(得分:0)

主要的错误是我没有将此过滤器添加到web.xml

<filter> <filter-name>OpenSessionInViewFilter</filter-name> 
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter‌​</filter-class> </filter> 
<filter-mapping> <
filter-name>OpenSessionInViewFilter</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping>

现在它很完美。