我遇到了将我的SQL数据库中的一些信息显示到Spring框架HTML表的问题。我一直在查找数据库中的项目时遇到错误,但它似乎无法找到或看到它。 这是错误:
2016-12-01 13:40:13.340 WARN 7968 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 42122, SQLState: 42S22
2016-12-01 13:40:13.340 ERROR 7968 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Column "LISTITEM0_.SHOPPING_LIST_ID" not found; SQL statement:
select listitem0_.id as id1_0_, listitem0_.contents as contents2_0_, listitem0_.created_utc as created_3_0_, listitem0_.is_checked as is_check4_0_, listitem0_.modified_utc as modified5_0_, listitem0_.priority as priority6_0_, listitem0_.shopping_list_id as shopping7_0_ from list_items listitem0_ [42122-192]
2016-12-01 13:40:13.374 ERROR 7968 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select listitem0_.id as id1_0_, listitem0_.contents as contents2_0_, listitem0_.created_utc as created_3_0_, listitem0_.is_checked as is_check4_0_, listitem0_.modified_utc as modified5_0_, listitem0_.priority as priority6_0_, listitem0_.shopping_list_id as shopping7_0_ from list_items listitem0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement] with root cause
org.h2.jdbc.JdbcSQLException: Column "LISTITEM0_.SHOPPING_LIST_ID" not found; SQL statement:
select listitem0_.id as id1_0_, listitem0_.contents as contents2_0_, listitem0_.created_utc as created_3_0_, listitem0_.is_checked as is_check4_0_, listitem0_.modified_utc as modified5_0_, listitem0_.priority as priority6_0_, listitem0_.shopping_list_id as shopping7_0_ from list_items listitem0_ [42122-192]
我的HTML文档在这里:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layouts/basic">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body layout:fragment="content">
<h1>Shopping List items</h1>
<table>
<thead>
<tr>
<th> </th>
<th>Content</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr th:each="listitem : ${list_items}">
<td th:text="${listitem.contents}"></td>
<td><form method="POST" ><input type="hidden" name="listId" th:value="${}"/><button type="submit">Delete</button></form></td>
</tr>
</tbody>
</table>
<a href="/">Back</a>
</body>
</html>
这也是我的Data.sql
insert into shoppinglist.lists (user_id, name, color, created_utc, modified_utc) values (1, 'Test List', '#000000', '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.lists (user_id, name, color, created_utc, modified_utc) values (2, 'Test List 2', '#000000', '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.lists (user_id, name, color, created_utc, modified_utc) values (3, 'Test List 3', '#000000', '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.lists (user_id, name, color, created_utc, modified_utc) values (4, 'Test List 4', '#000000', '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.list_items (list_id, contents, priority, is_checked, created_utc, modified_utc) values (1, 'Hello there', 1, false, '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.list_items (list_id, contents, priority, is_checked, created_utc, modified_utc) values (2, 'Hello there', 1, false, '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.list_items (list_id, contents, priority, is_checked, created_utc, modified_utc) values (3, 'Hello there', 1, false, '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.list_items (list_id, contents, priority, is_checked, created_utc, modified_utc) values (4, 'Hello there', 1, false, '2016-11-30 03:42:56', '2016-11-30 03:42:56');
insert into shoppinglist.notes (item_id, body, created_utc, modified_utc) values ('1', 'this is where my paragraph would go', '2016-11-30 03:42:56', '2016-11-30 03:42:56');
最后是我的控制器
@Controller
public class ListController {
@Autowired
private ListRepository listRepo;
@Autowired
private ListItemRepository listItemRepo;
@GetMapping("")
public String index(Model model, HttpServletRequest request) {
return "index";
}
@GetMapping("/ListsofLists")
public String List(Model model) {
model.addAttribute("lists", listRepo.findAll());
return "list_of_lists";
}
@GetMapping("/ListsofLists/{id}")
public String listitems(Model model, @PathVariable(name = "id") int id) {
model.addAttribute("id", id);
// never forget to import the proper beans!
ListItem u = listItemRepo.findOne(id);
model.addAttribute("list_items", u);
// yes I am going with listing the lists, I thought it would be funny.
return "list_list";
}
// GetMapping and PostMapping for editing items in lists.
@GetMapping("/ListsofLists/{id}/edit")
public String listEdit(Model model, @PathVariable(name = "id") int id) {
model.addAttribute("id", id);
List u = listRepo.findOne(id);
model.addAttribute("lists", u);
return "list_edit";
}
@PostMapping("/ListsofLists/{userId}/edit")
public String listEditSave(@ModelAttribute @Valid List list, BindingResult result, Model model) {
listRepo.save(list);
return "redirect:/ListsofLists/" + list.getId();
}
}
答案 0 :(得分:0)
我的搭档发现了错误。在今天早些时候的一系列重构之后,我们的一个bean中有一个变量。