notbugbutfeature
@notbugbutfeature
Типа вебушник

Не работает удаление записей из БД. Spring boot. В чем проблема?

Добрый день, Хабр! Я опять со своим приложением. Почему то не работает удаление записей из БД, приложение даже не запускается, говорит, что ошибка
Вот код с контроллера:
@GetMapping(value = "/clients/{clientId/delete}")
    public String clientDelete(@PathVariable("clientId") Integer clientId) {
        clientRepository.Clientdelete(clientId);
        return "redirect:/clients";


Вот ClientService:
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);
    }

}


Вот 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);
 }


Вот 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);
}


А вот собственно ошибка которую выдает (весь лог присылать не буду, так как я думаю это ключевая часть:
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!
  • Вопрос задан
  • 572 просмотра
Решения вопроса 1
LaRN
@LaRN
Senior Developer
Возможно причина в том, что нужно использовать метод deleteById, который уже есть в CrudRepository.
https://docs.spring.io/spring-data/commons/docs/cu...
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
azerphoenix
@azerphoenix Куратор тега Spring
Java Software Engineer
У вас тут ошибка:
@GetMapping(value = "/clients/{clientId/delete}")
Должно быть:
@GetMapping(value = "/clients/{clientId}/delete")

Внимательно читаем логи:

Error creating bean with name 'ladaController' defined
No property clientdelete found for type ClientEntity!
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы