0

I'm learning spring and I'm doing a basic project that consults a table and shows the rows through a service.

At the time of building the project I have the following error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personaService' defined in file [D:\Daniel\Proyecto-Catalogo\backend\cyberbuzon\target\classes\com\verasoftec\cyberbuzon\negocio\services\PersonaService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.verasoftec.cyberbuzon.modelo.Persona com.verasoftec.cyberbuzon.negocio.repository.PersonaRepository.edit(com.verasoftec.cyberbuzon.modelo.Persona)! No property edit found for type Persona!
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]

... enter image description here My class: enter image description here

enter image description here

enter image description here

enter image description here

I don't understand what I'm doing wrong.

izhang05
  • 744
  • 2
  • 11
  • 26
Daniel Vera
  • 77
  • 1
  • 10

2 Answers2

0

Your PersonaService is missing the @Autowired annotation. So your personaRepository can’t be created through dependency injection.

You have two options:

  1. You can annotate your personaRepository field with @Autowired
  2. or you can annotate your PersonaService constructor with @Autowired

Same goes for your PersonaResource and your personaService field.

I would recommend to annotate your constructor. See this post for more information about that.

So in the end your code should look like this:

@Service
@Transactional(readOnly = true)
public class PersonaService {

   private final PersonaRespository personaRepository;

   @Autowired
   public PersonaService(PersonaRepository personaRepository) {
      this.personaRepository = personaRepository;
   }

   ...
}

And your PersonaResource should look like this:

@RestController
@RequestMapping("/personas")
public class PersonaResource {

   private final PersonaService personaService;

   @Autowired
   public PersonaResource(PersonaService personaService) {
      this.personaService = personaService;
   }

   ...
}
Constantin Beer
  • 5,447
  • 6
  • 25
  • 43
  • Thank you for your answer. I made the adjustment according to what you tell me, but the problem remains. The same error is still displayed. – Daniel Vera Sep 11 '19 at 13:52
0

I think you forgot to create a PersonaService interface and packed the intended methods into your PersonaRepository instead. And the problem with the methods you put in there is that they don't follow the pattern of the JpaRepository. Take a look at this article to find out how to declare methods in your PersonaRepository.

To fix your problem you could easily just delete the methods in your PersonaRepository.

public interface PersonaRepository extends JpaRepository<Persona, Integer> { }

You already used the right method of your PersonaRepository in your PersonaService (personaRepository.findAll()), so you just have to add your methods that you removed from your PersonaRepository into your PersonaService and implement the corresponding methods from your repository like findById(int id), save(Persona persona) and delete(int id).

Constantin Beer
  • 5,447
  • 6
  • 25
  • 43