Error Creating bean with name 'transactionController'

Asked

Viewed 199 times

0

I’m starting to learn Spring and I’m getting this error, I’ve done several searches on the internet but so far I don’t understand what is causing the problem:

    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!

Class:

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"

                )
        );
    }

}

Repository

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

I am very grateful if someone can help me what is wrong in my code.

1 answer

0


In the TransactionRepoCustom, delete the method:

void deleteTransactionId(String id);

Like TransactionRepoCustom is the daughter of JpaRepository, she inherits deleteById(id). So, when you need to delete via id, you can use:

instanceRepository.deleteById(id)

Another thing. You can take @Service of the interface TransactionServiceCustom. Just leave it in the implementation class TransactionServiceImpl.

If you make new mistakes add the question to try to help.

  • Thank you very much, gave now the compilation is working. I’m having trouble now on the return of my GET method that is not bringing the data back, as no error returned I am trying to analyze what might be.

  • @Victor, in the code you sent, get is returning a list of an element you are creating in the service. The goal is the same or search a data in the database?

  • for the time being, I discovered that I was calling the interface in the controller rather than calling the implementation service. I made that change and now the list is returning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.