0

Estou começando aprender Spring e estou recebendo esse erro, já fiz diversas pesquisas na internet porém até o momento não entendi o que está causando o problema:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionController' defined in file [/Users/seuvito/Projetos/meubolso/target/classes/com/financas/meubolso/transaction/TransactionController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionServiceImpl': Unsatisfied dependency expressed through field 'transactionRepoCustom'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionRepoCustom' defined in com.financas.meubolso.transaction.TransactionRepoCustom 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 void com.financas.meubolso.transaction.TransactionRepoCustom.deleteTransactionId(java.lang.String)! No property deleteTransactionId found for type Transaction!

Classe:

package com.financas.meubolso.transaction;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table
public class Transaction {

    @Id()
    @Column(name = "id")
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String id;

    @Column(name = "title")
    private String title;

    @Column(name = "amount")
    private Double amount;

    @Column(name = "category")
    private String category;

    @Column(name = "type")
    private String type;

    public Transaction() {
    }

    public Transaction(String title, Double amount, String category, String type) {
        this.title = title;
        this.amount = amount;
        this.category = category;
        this.type = type;
    }

    public Transaction(String id, String title, Double amount, String category, String type) {
        this.id = id;
        this.title = title;
        this.amount = amount;
        this.category = category;
        this.type = type;
    }

    @Override
    public String toString() {
        return "Transaction{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", amount=" + amount +
                ", category='" + category + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}

Contoller:

package com.financas.meubolso.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(path = "api/v1/transactions")
public class TransactionController {
    private TransactionServiceCustom transactionServiceCustom;

    @Autowired
    public void setTransactionService(TransactionServiceCustom transactionServiceCustom) {
        this.transactionServiceCustom = transactionServiceCustom;
    }

    @Autowired
    public TransactionController(TransactionServiceCustom transactionServiceCustom) {
        this.transactionServiceCustom = transactionServiceCustom;
    }

    @RequestMapping(value = "transaction", method = RequestMethod.GET)
    public List<Transaction> ShowAllTransaction(TransactionServiceCustom transactionServiceCustom) {
        return transactionServiceCustom.getTransaction();
    }
}

Service:

package com.financas.meubolso.transaction;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface TransactionServiceCustom {
    void addTransaction(Transaction transaction);
    List<Transaction> getTransaction();
}

Service Impl

package com.financas.meubolso.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TransactionServiceImpl implements TransactionServiceCustom {

    @Autowired
    private TransactionRepoCustom transactionRepoCustom;

    @Transactional
    public void addTransaction(Transaction transaction) {
        transactionRepoCustom.saveAndFlush(transaction);
    }

    @Transactional
    public List<Transaction> getTransaction() {
        return List.of(
                new Transaction(
                        "id",
                        "aluguel",
                        10.00,
                        "habitacao",
                        "despesa"

                )
        );
    }

}

Repositório

package com.financas.meubolso.transaction;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface TransactionRepoCustom extends JpaRepository<Transaction, String> {

    void deleteTransactionId(String id);
    Optional<Transaction> findTransactionById(String id);
}

fico muito grato caso alguém consiga me ajudar o que está errado no meu código.

1
  • Usa o JpaRepository, ele herda outras interfaces, daí seria interessante usar o método deleteById(id) 30/09/2021 às 18:39

1 Resposta 1

0

No TransactionRepoCustom, apague o método:

void deleteTransactionId(String id);

Como TransactionRepoCustom é filha de JpaRepository, ela herda deleteById(id). Logo, quando você precisar de apagar via id, você pode usar:

instanceRepository.deleteById(id)

Outra coisa. Você pode retirar @Service da interface TransactionServiceCustom. Deixe apenas na classe de implementação TransactionServiceImpl.

Se der novos erros acrescente a pergunta para tentarmos ajudar.

3
  • muito obrigado, deu agora a compilação está funcionando. Estou com problema agora no retorno do meu método GET que não está trazendo os dados, como não retornou erro nenhum estou tentando analisar o que pode ser.
    – Victor
    7/06/2021 às 20:38
  • @Victor, no código que você enviou, o get está retornando uma lista de um elemento que você está criando no service. O objetivo é esse mesmo ou buscar um dado no banco de dados? 7/06/2021 às 20:55
  • por enquanto é isso mesmo, descobri que eu tava chamando a interface no controller ao invés de chamar o serviço de implementação. Fiz essa alteração e agora o a lista está retornando.
    – Victor
    7/06/2021 às 20:56

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .