Spring CrudRepository不工作save&删除操作

时间:2017-11-15 11:24:50

标签: java spring spring-mvc spring-data spring-data-jpa

在Spring中,CrudRepository findAll()操作适用于从数据库中获取数据,但在保存,更新和更新的情况下具有相同的配置。删除它不起作用。

EmployeeService.java

 @Service
    public class EmployeeService {

        @Autowired
        private EmployeeRepo employeeRepoI;

        @Transactional
        public List<Employee> getAllEmployee() {
            return (List<Employee>) employeeRepoI.findAll();
        }

        @Transactional
        public Employee getEmployee(int id) {
            return (Employee) employeeRepoI.findOne(id);
        }

        @Transactional
        public Employee addEmployee(Employee employee) {
            return (Employee) employeeRepoI.save(employee);
        }

        @Transactional
        public Employee updateEmployee(Employee employee) {
            return (Employee) employeeRepoI.save(employee);

        }

        @Transactional
        public void deleteEmployee(int id) {
             employeeRepoI.delete(id);
        }

    }

EmployeeRapo.java

@Repository
public interface EmployeeRepo<T, ID extends Serializable> extends CrudRepository<Employee, Long> {

    List<Employee> findAll();


}

2 个答案:

答案 0 :(得分:0)

你有Long类型的CrudRepository和原始int的deleteEmployee。这个值应该匹配。

答案 1 :(得分:0)

正如 @Sergey 所指出的,您的EmployeeRepo定义错误

试试这个

@Repository
public interface EmployeeRepo extends CrudRepository<Employee, Long> {
    List<Employee> findAll();
}

另外,deleteEmployee()方法需要int,而Long方法则需要@Transactional public void deleteEmployee(Long id) { employeeRepoI.delete(id); } 作为参数。

 taggedClothes: { 
      0: {id:0, left:100, top:100, thumbSize:100}, <- I want to setState this
      1: {id:1, left:200, top:200, thumbSize:200},
 },