L
L
Leo Sobolev2021-05-13 16:18:22
PostgreSQL
Leo Sobolev, 2021-05-13 16:18:22

Deleting records from the database does not work. springboot. What is the problem?

Good afternoon, Habr! I'm back with my app. For some reason, deleting records from the database does not work, the application does not even start, it says that the error is
Here is the code from the controller:

@GetMapping(value = "/clients/{clientId/delete}")
    public String clientDelete(@PathVariable("clientId") Integer clientId) {
        clientRepository.Clientdelete(clientId);
        return "redirect:/clients";


Here is the Client Service:
package com.example.karakum.service.impl;

import com.example.karakum.persist.entity.ClientEntity;
import com.example.karakum.repo.ClientRepository;
import com.example.karakum.service.IClientService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ClientService implements IClientService {

    private final ClientRepository clientRepository;
    private Integer clientId;

    public ClientService(ClientRepository clientRepository) {
        this.clientRepository = clientRepository;
    }

    @Override
    public Optional<ClientEntity> findById(Integer clientId) {
        //                                   * -- Следи за названиями имятаблицыId, чтобы были одинаковы, А тут Integer всегда!
        return clientRepository.findById(clientId);
    }

    @Override
    public List<ClientEntity> getAllClients() {
        return clientRepository.getAll();
    }
 
    @Override
    public Optional<ClientEntity> ClientDelete(Integer clientId) {
        return clientRepository.Clientdelete(clientId);
    }

}


Here is the IClientService:
package com.example.karakum.service;

import com.example.karakum.persist.entity.ClientEntity;

import java.util.List;
import java.util.Optional;

public interface IClientService {
    List<ClientEntity> getAllClients();

    Optional<ClientEntity> findById(Integer clientId);
 Optional<ClientEntity> ClientDelete(Integer clientId);
 }


Here is the ClientRepository:
package com.example.karakum.repo;

import com.example.karakum.persist.entity.ClientEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

public interface ClientRepository extends CrudRepository <ClientEntity, Integer> {
    @Query(value = "select h from ClientEntity h")
    List<ClientEntity> getAll();
    Optional<ClientEntity> Clientdelete(Integer clientId);
}


And here is the actual error that it produces (I will not send the entire log, since I think this is the key part:
Error creating bean with name 'ladaController' defined in file [C:\application\karakum\target\classes\com\example\karakum\LadaController.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService' defined in file [C:\application\karakum\target\classes\com\example\karakum\service\impl\ClientService.class] : Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientRepository' defined in com.example.karakum.repo.ClientRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.example.karakum.repo.ClientRepository.Clientdelete(java.lang.Integer)! No property clientdelete found for type ClientEntity!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2021-05-13
@notbugbutfeature

Perhaps the reason is that you need to use the deleteById method, which is already in the CrudRepository.
https://docs.spring.io/spring-data/commons/docs/cu...

O
Orkhan Hasanli, 2021-05-14
@azerphoenix

You have an error here:
@GetMapping(value = "/clients/{clientId/delete}")
It should be:
@GetMapping(value = "/clients/{clientId}/delete")
Carefully read the logs:


Error creating bean with name 'ladaController' defined
No property clientdelete found for type ClientEntity!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question