【问题标题】:Spring hibernate One to Many not showing春季休眠一对多不显示
【发布时间】: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


【解决方案1】:

移除@Transient 注解。此注解指定不保留带注解的字段。

【讨论】:

  • 数据库中是否有数据。因为以前不可能有。
  • 是的,我插入了数据,我犯了一个错误,这就是为什么没有改变,现在我收到以下错误:无法确定类型:com.bookr.app.model.Book,在桌子上:审查,列:[org.hibernate.mapping.Column(book)]。我认为这与私人图书有关; Review 类的声明,但我不确定
  • 这是完整的错误消息:在类 com.bookr.app.config.ApplicationContextConfig 中创建名称为“sessionFactory”的 bean 时出错:bean 的实例化失败;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException: 工厂方法 [public org.hibernate.SessionFactory com.bookr.app.config.ApplicationContextConfig.getSessionFactory(javax.sql.DataSource)] 抛出异常;嵌套异常是 org.hibernate.MappingException:无法确定类型:com.bookr.app.model.Book,表:REVIEW,列:[org.hibernate.mapping.Column(book)]
  • 请删除 JoinColumn 的东西,只要你不是 100% 确定你需要它。我想我需要写一个 JPA 如何写博客。
  • 现在我的代码如下所示:Book.java 中的 @OneToMany public Set&lt;Review&gt; getReviews() { return review; } 和 Review.java 中的 @ManyToOne public Book getBook() { return book; } 我得到错误:Could not determine type for: java.util.Set, at table: REVIEW, for columns: [org.hibernate.mapping.Column(reviews)]
【解决方案2】:

您的映射本身似乎有问题。

请看一下这个blog post。我想它可能会对你有所帮助。

【讨论】:

  • 我按照指南进行操作,但出现此错误:R_BOOKID(应使用 insert="false" update="false" 映射)。在我在 r_bookid 的 @Column 注释中添加 insertable=false 和 updatable=false 之后,我得到: org.springframework.web.servlet.PageNotFound noHandlerFound 警告:在 DispatcherServlet 中找不到带有 URI [/app/] 的 HTTP 请求的映射,名称为'SpringDispatcher'
  • 您是否使用带有休眠功能的spring mvc?我认为您正在点击 url,但该 req 未使用任何 servlet 方法映射
  • 是的,我正在使用,如果您认为有帮助,我可以上传整个项目,或者我可以添加更多代码
  • public class SpringWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.register(ApplicationContextConfig.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet( "SpringDispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
相关资源
最近更新 更多