user8799994
user8799994

Reputation:

Exception evaluating SpringEL expression, spring guru course

Book.java

package pl.spring.guru.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String title;
    private String isbn;
    @OneToOne
    private Publisher publisher;

   @ManyToMany
   @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = 
   "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id"))

   private Set<Author> authors = new HashSet<>();

   public Book() {
   }

   public Book(String title, String isbn, Publisher publisher){
       this.title=title;
       this.isbn=isbn;
       this.publisher=publisher;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getIsbn() {
      return isbn;
  }

   public void setIsbn(String isbn) {
      this.isbn = isbn;
  }

  public Publisher getPublisher() {
      return publisher;
  }

  public void setPublisher(Publisher publisher) {
     this.publisher = publisher;
  }

  public Set<Author> getAuthors() {
     return authors;
  }

  public void setAuthors(Set<Author> authors) {
     this.authors = authors;
  }
}

BookController.java

package pl.spring.guru.spring5webapp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pl.spring.guru.spring5webapp.repositories.BookRepository;

@Controller
public class BookController {

    private BookRepository bookRepository;

    public BookController(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @RequestMapping(value = "/books")
    public String getBooks(Model model){
        model.addAttribute("books",bookRepository.findAll());

    return "books";
  }
}

books.html - thymeleaf template

<!DOCTYPE html>
<html lang="en" xmlns:th=”http://www.thymeleaf.org”>
<head>
    <meta charset="UTF-8">
    <title>Spring Framework Guru</title>
</head>
<body>
<h1>Books list</h1>
<table>
   <tr>
       <th>ID</th>
       <th>Title</th>
       <th>Author</th>
   </tr>
   <tr th:each="book : ${books}">
        <td th:text="${book.id}">123</td>
       <td th:text="${book.title}">Spring in Action</td>
       <td th:text="${book.publisher.name}">Wrox</td>
   </tr>
</table>

</body>
</html>

when I'm trying go to localhost/books i get a

Exception evaluating SpringEL expression: "book.id" (template: "books" - line 16, col 13)

I'm just learning the spring and I'm starting to use the spring framework guru tutorial. I don't understand too, why adding a empty constructor in book.java is require. Without it i have next problem:

No default constructor for entity: : pl.spring.guru.spring5webapp.model.Book; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : pl.spring.guru.spring5webapp.model.Book

Upvotes: 1

Views: 1303

Answers (1)

Quagaar
Quagaar

Reputation: 1287

You're missing a getter for id. Without that, the template engine cannot access it.

As for the default constructor - it is required by Spring to create an instance of the Entity. How should Spring know what parameters to use if no default constructor was available.

Apart from the tutorial you should take a look at the Spring Framework reference docs. It's a lot to read but especially the DI basic are explained quite good.

Upvotes: 2

Related Questions