I'm trying to get data from an H2 database to load up on my JSP pages, and the program just isn't having it. Now, data is being loaded onto the database, its just not coming back out. I try to load a page of book information, and all that comes out is this error:
> Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "BOOK" not found; SQL statement:
> select book0_.ID as id1_1_, book0_.AUTHOR as author2_1_, book0_.GENRE as genre3_1_, book0_.ILLUSTRATOR as illustra4_1_, book0_.SERIES as series5_1_, book0_.TITLE as title6_1_ from BOOK book0_ [42102-214]
EDIT: As per the project's instructions, we're explicitly not allowed to use JPA.
Here's (what I think is) the relevant code:
the Model
``
> package org.bookarchive.model;
>
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.Table;
>
> @Entity
> @Table(name = "BOOK")
> public class Book {
>
> @Id
> @GeneratedValue(strategy = GenerationType.SEQUENCE)
> @Column(name = "ID")
> private Long id;
>
> @Column(name = "TITLE", nullable = false)
> private String title;
>
> @Column(name = "SERIES")
> private String series;
>
> @Column(name = "AUTHOR", nullable = false)
> private String author;
>
> @Column(name = "ILLUSTRATOR")
> private String illustrator;
>
> @Column(name = "GENRE")
> private String genre;
>
> public Long getId() {
> return id;
> }
>
> public void setId(Long id) {
> this.id = id;
> }
>
> public String getTitle() {
> return title;
> }
>
> public void setTitle(String title) {
> this.title = title;
> }
>
> public String getSeries() {
> return series;
> }
>
> public void setSeries(String series) {
> this.series = series;
> }
>
> public String getAuthor() {
> return author;
> }
>
> public void setAuthor(String author) {
> this.author = author;
> }
>
> public String getIllustrator() {
> return illustrator;
> }
>
> public void setIllustrator(String illustrator) {
> this.illustrator = illustrator;
> }
>
> public String getGenre() {
> return genre;
> }
>
> public void setGenre(String genre) {
> this.genre = genre;
> }
>
> @Override
> public int hashCode() {
> final int prime = 31;
> int result = 1;
> result = prime * result (int) (id ^ (id >>> 32));
> return result;
> }
>
> @Override
> public boolean equals(Object obj) {
> if (this == obj)
> return true;
> if (obj == null)
> return false;
> if (!(obj instanceof Book))
> return false;
> Book other = (Book) obj;
> if (id != other.id)
> return false;
> return true;
> }
>
> @Override
> public String toString() {
> return "Book [id=" id ", title=" title ", series=" series ", author=" author ", illustrator="
> illustrator ", genre=" genre "]";
> }
>
> }
the Controller
> @RestController
> @RequestMapping("/api/favoritebooks")
> @Configuration
> @ComponentScan("org.bookarchive")
> public class RestListController {
>
> Logger logger = LoggerFactory.getLogger(RestListController.class);
>
> @Autowired
> private ListService bookList;
>
>
> ModelAndView mv = new ModelAndView("bookList");
>
> @GetMapping
> public ModelAndView getBookListHome() {
> List<Book> books = bookList.findAllBooks();
> mv.addObject("books", books);
> return mv;
> }
the DAO
package org.bookarchive.dao; import java.util.List; import javax.persistence.Query; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import org.bookarchive.model.Book; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class DAOImpl implements DAO { @Autowired private SessionFactory sessionFactory; @SuppressWarnings("unchecked") @Override public List<Book> findAllBooks() { Session s = sessionFactory.getCurrentSession(); CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaQuery<Book> cq = cb.createQuery(Book.class); Root<Book> root = cq.from(Book.class); cq.select(root); Query query = s.createQuery(cq); return query.getResultList(); }
the javascript Service
> 'use strict';
>
> angular.module('myApp').factory('ListService', ListServiceFactory)
>
> ListServiceFactory.$inject = ['$http', '$log']
>
> function ListServiceFactory($http, $log) {
> let REST_SERVICE_URI = 'http://localhost:8081/favoritebooks/';
>
> let factory = {
> fetchAllBooks: fetchAllBooks,
> createBook: createBook,
> updateBook:updateBook,
> deleteBook:deleteBook
> };
>
> return factory;
>
> function fetchAllBooks() {
> return $http.get(REST_SERVICE_URI).then(
> function (response) {
> return response.data;
> },
> function (errResponse) {
> $log.error('Error while fetching Books ', errResponse);
> }
> );
> }
the javascript Controller
> 'use strict';
>
> angular.module('myApp').controller('RestListController', ['$scope', '$log' ,'ListService', function($scope, $log, ListService) {
> var self = this;
> self.book = { id: null, title: '', series: '', author: '', illustrator: '', genre: ''};
> self.books = [];
>
> self.submit = submit;
> self.edit = edit;
> self.remove = remove;
> self.reset = reset;
>
>
> findAllBooks();
>
> function findAllBooks(){
> ListService.fetchAllBooks()
> .then(
> function(d) {
> self.books = d;
> },
> function(errResponse){
> $log.error('Error while fetching Books ', errResponse);
> }
> );
> }
and the JSP where it should be coming out
> page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
> taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
> <!DOCTYPE html>
> <html>
> <head>
> <title>Book Archive</title>
> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
> <link href="/static/css/app.css" rel="stylesheet"></link>
> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.js"></script>
> </head>
>
> <body>
>
> <div align="center">
> <h2>Complete Book List</h2><BR>
>
> <table border="1">
> <tr>
> <th>Book Id</th>
> <th>Title</th>
> <th>Series</th>
> <th>Author</th>
> <th>Illustrator</th>
> <th>Genre</th>
> </tr>
> <c:forEach var="book" items="${books}">
> <tr>
> <td>${book.id}</td>
> <td>${book.title}</td>
> <td>${book.series}</td>
> <td>${book.author}</td>
> <td>${book.illustrator}</td>
> <td>${book.genre}</td>
> <td>
> <button type="button" ng-click="ctrl.edit(book)" >Edit</button>
> <button type="button" ng-click="ctrl.remove(book)" >Remove</button>
> </td>
> </tr>
> </c:forEach>
> </table>
> </div>
> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.js"></script>
> <script src="/static/js/app.js"></script>
> <script src="/static/js/service/listService.js"></script>
> <script src="/static/js/controller/listController.js"></script>
>
> </body>
> </html>
Any help at all would be much appreciated.
I've tried various combinations of altered syntax, updated dependencies, and database configurations. An early version of the program without the database worked just fine, and the database is receiving data (basic schema and data SQL files) but I can't seem to get that connection with the client side of things.
CodePudding user response:
Looking at your current configuration, you are not creating the Book table anywhere. In order for it to be auto-generated (which is a situation you might want in PoC projects or in the testing part of your app) you can add spring.jpa.hibernate.ddl-auto=create-drop
inside your application.properties file (inside your resources folder).
Check this out for more too!
UPDATE: You are already using JPA. What Hibernate is, is literally an implementation of the JPA (abstract) specifications. Therefore, you have to use JDBC
directly, check this out to learn more.
Happy hacking =)!
CodePudding user response:
Alright, mracheriker's suggestion pointed me in the right direction to find the answer (turns out it was a problem with the table name), so thank you very much!