简体   繁体   中英

Spring CrudRepository not working save & delete operation

In Spring, CrudRepository findAll() operation working good for fetching data from the database but with the same configuration in case of saving, update & delete it's not working.

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();


}

You have CrudRepository with Long type and deleteEmployee with primitive int. This values should match.

As pointed out by @Sergey Your EmployeeRepo has a wrong definition there

Try this

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

Also your deleteEmployee() method takes an int while it should take Long as a parameter.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM