【发布时间】:2018-02-13 09:23:26
【问题描述】:
您好,我正在使用 spring mvc 使用经典方法作为表单类型来测试一个快速的 crud
错误是这样的
org.springframework.dao.EmptyResultDataAccessException: No class
com.project.springinventory.entity.Customers entity with id 0 exists!
at
org.springframework.data.jpa.repository.support.SimpleJpaRepository.delete(SimpleJpaRepository.java:154) ~[spring-data-jpa-1.11.10.RELEASE.jar:na]
看起来对象没有收到任何东西
检查了一切,看起来还不错,我不知道该怎么办
如您所见,我正在接收 id 并从 DB 获取所有字段 有没有一种方法可以在不改变的情况下使用这种方法? (使用表格)
查看
<table class="table table-bordered">
<tr>
<th><span>Id</span></th>
<th><span>Course</span></th>
<th><span>Name</span></th>
<th><span>Last Name</span></th>
<th><span>Address</span></th>
<th><span>Actions</span></th>
</tr>
<tr th:each="customer:${customers}">
<th><span th:text="${customer.id}"></span></th>
<th><span th:text="${customer.firstname}"></span></th>
<th><span th:text="${customer.lastname}"></span></th>
<th><span th:text="${customer.course}"></span></th>
<th><span th:text="${customer.address}"></span></th>
<th>
<form th:action="@{/showCustomer}" th:object="${customers}" method="post">
<input type="submit" name="btnInsert" class="btn btn-primary" value="Add" />
</form>
<form th:action="@{/UpdateCustomer}" th:object="${customers}" method="post">
<button class="btn btn-warning">
<span class="glyphicon glyphicon-pencil" aria-hidden="true">Edit</span>
</button>
</form>
<form th:action="@{/DeleteCustomer}" th:object="${customers}" method="post">
<input type="submit" name="btnInsert" class="btn btn-primary" value="Delete" />
</form>
</th>
</tr>
<form action="#" th:action="@{/customermanagement}" th:object="${CustomersAdd}" method="post">
<tr>
<td><input type="text" placeholder="Course"
th:field="*{course}" /></td>
<td><input type="text" placeholder="Name"
th:field="*{firstname}" /></td>
<td><input type="text" placeholder="Last Name"
th:field="*{lastname}" /></td>
<td><input type="text" placeholder="Address"
th:field="*{address}" /></td>
<td><input type="submit" name="btnInsert"
class="btn btn-primary" value="Add" /></td>
</tr>
<tr>
<td>
<p th:if="${#fields.hasErrors('course')}" th:errors="*{course}">course
has errors</p>
</td>
<td>
<p th:if="${#fields.hasErrors('firstname')}"
th:errors="*{firstname}">firstname has errors</p>
</td>
<td>
<p th:if="${#fields.hasErrors('lastname')}"
th:errors="*{lastname}">lastname has errors</p>
</td>
<td>
<p th:if="${#fields.hasErrors('address')}"
th:errors="*{address}">address has errors</p>
</td>
</tr>
</form>
</table>
控制器
@PostMapping("/DeleteCustomer")
public ModelAndView DeleteUser(@ModelAttribute("customers") Customers
Customers) {
ModelAndView mav = new ModelAndView();
System.err.println("Deleting:");
System.err.println("getId " + Customers.getId());
System.err.println("getCourse " + Customers.getCourse().toString());
System.err.println("getCourse " + Customers.getFirstname().toString());
System.err.println("getLastname " + Customers.getLastname().toString());
System.err.println("getAddress " + Customers.getAddress().toString());
customersService.removeCustomer(Customers.getId());
customersService.listAllCustomers();
mav.setViewName("redirect:/" + MAIN_VIEW);
return mav;
}
SERVICE
public interface CustomersService {
public abstract List<Customers> listAllCustomers();
public abstract Customers addCustomer(Customers customers);
public abstract int removeCustomer(int id);
public abstract Customers updateCustomer(Customers customers);
}
服务实施
package com.project.springinventory.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.project.springinventory.entity.Customers;
import com.project.springinventory.repository.CustomersJpaRepository;
@Service("customersServiceImpl")
public class CustomersServiceImpl implements CustomersService {
@Autowired
@Qualifier("customersJpaRepository")
private CustomersJpaRepository customersJpaRepository;
@Override
public List<Customers> listAllCustomers() {
// TODO Auto-generated method stub
return customersJpaRepository.findAll();
}
@Override
public Customers addCustomer(Customers customers) {
// TODO Auto-generated method stub
return customersJpaRepository.save(customers);
}
@Override
public int removeCustomer(int id) {
// TODO Auto-generated method stub
customersJpaRepository.delete(id);
return 0;
}
@Override
public Customers updateCustomer(Customers customers) {
// TODO Auto-generated method stub
return customersJpaRepository.save(customers);
}
''
}
实际上一切正常,删除除外
【问题讨论】:
-
发布您的
Customers实体。
标签: java spring spring-mvc thymeleaf