【发布时间】:2015-07-13 04:40:04
【问题描述】:
我正在尝试构建一个具有两种对象的网络应用程序:书籍和评论(一本书可以有多个评论)。
我可以看到书籍列表,但是当我进入书籍页面时,我可以看到书籍的标题和其他属性,但评论的属性不会显示,我不知道为什么。我只能看到一个带有空值的单行的空表。
我认为它无法识别该对象,因为审查表中有三个条目,所以如果它能够识别该表,那么该表就有三行。
Book.java
@Entity
@Table(name = "BOOK")
public class Book {
private int bookId;
private int bookRating;
private String bookCategory;
private String bookAuthor;
private String bookTitle;
@Transient
private Set<Review> review;
@Transient
private List<String> reviews;
public Book() {}
public Book(String title) {
this.bookTitle = title;
}
@Id
@GeneratedValue
@Column(name = "BOOKID")
public int getBookid() {
return bookId;
}
...
@OneToMany
@JoinColumn(name="r_bookid")
public Set<Review> getReviews() {
return review;
}
public void setReviews(Set<Review> reviews) {
this.review = review;
}
}
Review.java
@Entity
@Table(name = "REVIEW")
public class Review {
private int id;
private Date r_date;
private String r_author;
private String r_text;
private int r_rating;
@Transient
private Set<Review> review;
private Book book;
private int r_bookid;
public Review(String author, String text, Date date, int rating, int bookid, Book book) {
super();
this.r_author = author;
this.r_text = text;
this.r_date = date;
this.r_rating = rating;
this.r_bookid = bookid;
this.book= book;
}
@Id
@GeneratedValue
@Column(name = "ID")
public int getId() {
return id;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "book", cascade = CascadeType.ALL)
public Set<Review> getReviews() {
return review;
}
BookDAOImpl.java
@Override
@Transactional
public Set<Review> getReviews() {
Set<Review> listReview = (Set<Review>) sessionFactory.getCurrentSession()
.createCriteria(Review.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return listReview;
}
Controller.java
@RequestMapping(value = "/book", method = RequestMethod.GET)
public ModelAndView viewBook(HttpServletRequest request) {
int bookId = Integer.parseInt(request.getParameter("id"));
Book book = bookDao.get(bookId);
Set<Review> review = book.getReviews();
ModelAndView model = new ModelAndView("viewBook");
model.addObject("book", book);
model.addObject("review", review);
return model;
}
viewBook.jsp
<h1>Carte: ${book.bookTitle}</h1>
<h2>Autor: ${book.bookAuthor}</h2>
<c:forEach var="rev" items="${review}" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${rev.r_author}</td>
<td>${rev.r_text}</td>
<td>${rev.r_date}</td>
<td>${rev.r_rating}</td>
</tr>
</c:forEach>
数据库表
CREATE DATABASE test;
CREATE TABLE `book` (
`bookId` int(11) NOT NULL AUTO_INCREMENT,
`bookTitle` varchar(45) NOT NULL,
`bookAuthor` varchar(45) NOT NULL,
`bookCategory` varchar(45) NOT NULL,
`bookRating` int(11) NOT NULL,
PRIMARY KEY (`bookId`)
);
CREATE TABLE `review` (
`id` int(11) NOT NULL,
`r_author` varchar(45) NOT NULL,
`r_text` varchar(205) NOT NULL,
`r_rating` int(11) NOT NULL,
`r_date` date NOT NULL,
`r_bookid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `r_bookid_idx` (`r_bookid`),
CONSTRAINT `r_bookid` FOREIGN KEY (`r_bookid`) REFERENCES `book` (`bookId`) ON DELETE CASCADE
);
【问题讨论】:
-
@Transient 私有集
审查。从 Review 类中删除此代码。并从 Book Class 的注释中删除 Transient -
我从两个类中删除了注释并从 Review 类中删除了 Set
方法,但现在我收到以下错误:无法确定类型:com.bookr.app.model.Book,在表:REVIEW,列:[org.hibernate.mapping.Column(book)] -
修复了一些措辞、语法高亮,并删除了 please/thanks。
标签: java spring hibernate mapping one-to-many