Error injecting dependency into Springboot

Asked

Viewed 2,006 times

0

I’m on the following error someone could help:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addAlunoEBController': Unsatisfied dependency expressed through field 'addAlunoService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addAlunoEBServiceImpl': Unsatisfied dependency expressed through field 'aebr'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addAlunoEBRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query method public abstract java.lang.Iterable com.borges.igreja.repository.AddAlunoEBRepository.findByAluno(com.borges.igreja.model.EscolaBiblica)! No student Property found for type Addalunoeb! Did you Mean 'students'?

Follows the classes to evaluate

Model class

    package com.borges.igreja.model;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    @Entity
    @Table(name="addalunoeb")
    public class AddAlunoEB {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long Id;

        @ManyToOne
        private Membros alunos;

        @ManyToOne
        private EscolaBiblica escolaBiblica;

        private String dtInicioEB;

        public Long getId() {
            return Id;
        }

        public void setId(Long id) {
            Id = id;
        }

        public Membros getAlunos() {
            return alunos;
        }

        public void setAlunos(Membros alunos) {
            this.alunos = alunos;
        }

        public EscolaBiblica getEscolaBiblica() {
            return escolaBiblica;
        }

        public void setEscolaBiblica(EscolaBiblica escolaBiblica) {
            this.escolaBiblica = escolaBiblica;
        }

        public String getDtInicioEB() {
            return dtInicioEB;
        }

        public void setDtInicioEB(String dtInicioEB) {
            this.dtInicioEB = dtInicioEB;
        }
    }

Repository class

package com.borges.igreja.repository;

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

    import com.borges.igreja.model.AddAlunoEB;
    import com.borges.igreja.model.EscolaBiblica;

    @Repository
    public interface AddAlunoEBRepository extends JpaRepository<AddAlunoEB, Long> {
        Iterable<AddAlunoEB> findByAluno(EscolaBiblica escolaBiblica);

    }

Service class

    package com.borges.igreja.service;

import java.util.List;

import com.borges.igreja.model.AddAlunoEB;
import com.borges.igreja.model.EscolaBiblica;

public interface AddAlunoService {

    List<AddAlunoEB> listarTodos();

    AddAlunoEB listarId(Long id);

    AddAlunoEB cadastrar(AddAlunoEB addAlunoEB);

    AddAlunoEB atualizar(AddAlunoEB addAlunoEB );

    void remover (Long id);

    Iterable<AddAlunoEB> listAluno(EscolaBiblica escolaBiblica);
}

Classe Impl

    package com.borges.igreja.service.impl;

import java.util.List;

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

import com.borges.igreja.model.AddAlunoEB;
import com.borges.igreja.model.EscolaBiblica;
import com.borges.igreja.repository.AddAlunoEBRepository;
import com.borges.igreja.service.AddAlunoService;

@Service
public class AddAlunoEBServiceImpl implements AddAlunoService {

    @Autowired
    private AddAlunoEBRepository aebr;

    @Override
    public List<AddAlunoEB> listarTodos() {
        return this.aebr.findAll();
    }

    @Override
    public AddAlunoEB listarId(Long id) {
        return this.aebr.findOne(id);
    }

    @Override
    public AddAlunoEB cadastrar(AddAlunoEB addAlunoEB) {
        return this.aebr.save(addAlunoEB);
    }

    @Override
    public AddAlunoEB atualizar(AddAlunoEB addAlunoEB) {
        return this.aebr.save(addAlunoEB);
    }

    @Override
    public void remover(Long id) {
        this.aebr.delete(id);

    }

    @Override
    public Iterable<AddAlunoEB> listAluno(EscolaBiblica escolaBiblica) {

        return this.aebr.findByAluno(escolaBiblica);
    }

}

Controller class

    package com.borges.igreja.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.borges.igreja.model.AddAlunoEB;
import com.borges.igreja.model.EscolaBiblica;
import com.borges.igreja.repository.AddAlunoEBRepository;
import com.borges.igreja.service.AddAlunoService;
import com.borges.igreja.service.EscolaBiblicaService;
import com.borges.igreja.service.MembrosService;


@Controller
@RequestMapping("/addAlunoEB")
public class AddAlunoEBController {

    @Autowired
    private EscolaBiblicaService escolaBiblicaService;

    @Autowired
    private MembrosService membrosService;

    @Autowired
    private AddAlunoService addAlunoService;

    @Autowired
    private AddAlunoEBRepository adr;

    @GetMapping
    public ModelAndView listarTodos() {
        ModelAndView modelAndView = new ModelAndView("addAlunoEB/listarAlunoEB");
        modelAndView.addObject("addAlunos", adr.findAll());
        return modelAndView;
    }

    @GetMapping("/{id}")
    public  ModelAndView addAluno(@PathVariable Long id, AddAlunoEB alunoEB) {
        EscolaBiblica escolaBiblica = escolaBiblicaService.listarId(id);
        ModelAndView modelAndView = new ModelAndView("addAlunoEB/AddAlunoEB");
        modelAndView.addObject(alunoEB);
        modelAndView.addObject("alunos", membrosService.listarTodos());
        modelAndView.addObject("escolasBiblicas", escolaBiblica);

        //Iterable<AddAlunoEB> addAlunoEB = addAlunoService.listAluno(escolaBiblica);
        Iterable<AddAlunoEB> iterable = addAlunoService.listAluno(escolaBiblica);
        modelAndView.addObject("addAlunosEB", iterable);
        return modelAndView;


    }

    @PostMapping("/{id}")
    public String addAlunoPost(@PathVariable Long id,AddAlunoEB addAlunoEB) {
        EscolaBiblica escolaBiblica = escolaBiblicaService.listarId(id);
        addAlunoEB.setEscolaBiblica(escolaBiblica);
        adr.save(addAlunoEB);
        return "redirect:/addAlunoEB/{id}";
    }

}

Thank you

1 answer

0

It seems that JPA is trying to find a student attribute in the Addalunoeb model. Your find is by Escolabiblica. My suggestion is to change the name of the method to findByEscolaBiblica.

  • Thanks @Patricia Espada, was really this did not know that would influence the name is changed Vlw for help.

  • I am glad that the solution served. If you can, vote yes in my reply. Thank you

  • I tried but as I have now registered on the site I do not have enough reputation points, sorry.

Browser other questions tagged

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