Home > Back-end >  How to solve this Spring Boot problem when I want to use a Repository
How to solve this Spring Boot problem when I want to use a Repository

Time:06-09

For my studies, I have a Spring Boot project using Hibernate and TomCat.

I wanted to add a Repository for one of the tables of my DB, but it created a bug that I can not solve despite many research.

The error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.epul.repository.ApprenantRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
    org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:326)
    org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:305)
    org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:82)
    org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58)
    org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:120)
    org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:76)
    org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:74)
    org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121)
    org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.base/java.lang.Thread.run(Thread.java:832)

Interface Repository :

package com.epul.repository;
 
import org.springframework.data.jpa.repository.JpaRepository;
import com.epul.domain.UtilisateurEntity;
import org.springframework.stereotype.Repository;
 
@Repository
public interface ApprenantRepository extends JpaRepository<UtilisateurEntity,Long>{
 
}

Service Interface :

package com.epul.dao;
 
import com.epul.domain.UtilisateurEntity;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public interface IServiceApprenant {
 
    public void insererApprenant(UtilisateurEntity apprenant);
 
    public List<UtilisateurEntity> listeApprenants() throws Exception;
 
    public UtilisateurEntity getUtilisateur(String login);
 
}

The Service class that implements the Interface :

package com.epul.dao;
 
import com.epul.domain.UtilisateurEntity;
import com.epul.meserreurs.MonException;
import com.epul.repository.ApprenantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
 
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;
 
@Service
public class ServiceApprenant extends EntityService implements IServiceApprenant{
 
    @Autowired
    private ApprenantRepository apprenantRepository;
 
    @Override
    public void insererApprenant(UtilisateurEntity apprenant) {
        apprenantRepository.save(apprenant);
    }
 
    @Override
    public List<UtilisateurEntity> listeApprenants() throws Exception {
       ...
    }
 
    @Override
    public UtilisateurEntity getUtilisateur(String login) throws MonException
    {
        ...
    }
}

UtilisateurEntity :

package com.epul.domain;

import javax.persistence.*;

@Entity
@Table(name = "utilisateur", schema = "projetpermis1", catalog = "")
public class UtilisateurEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "NumUtil", nullable = false)
    private int numUtil;
    @Basic
    @Column(name = "NomUtil", nullable = false, length = 100)
    private String nomUtil;
    @Basic
    @Column(name = "MotPasse", nullable = false, length = 100)
    private String motPasse;
    @Basic
    @Column(name = "salt", nullable = false, length = 100)
    private String salt;
    @Basic
    @Column(name = "role", nullable = false, length = 100)
    private String role;
    @Basic
    @Column(name = "email", nullable = true, length = 255)
    private String email;
    @Basic
    @Column(name = "surname", nullable = true, length = 50)
    private String surname;
    @Basic
    @Column(name = "forename", nullable = true, length = 50)
    private String forename;

    public int getNumUtil() {
        return numUtil;
    }

    public void setNumUtil(int numUtil) {
        this.numUtil = numUtil;
    }

    public String getNomUtil() {
        return nomUtil;
    }

    public void setNomUtil(String nomUtil) {
        this.nomUtil = nomUtil;
    }

    public String getMotPasse() {
        return motPasse;
    }

    public void setMotPasse(String motPasse) {
        this.motPasse = motPasse;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getForename() {
        return forename;
    }

    public void setForename(String forename) {
        this.forename = forename;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UtilisateurEntity that = (UtilisateurEntity) o;

        if (numUtil != that.numUtil) return false;
        if (nomUtil != null ? !nomUtil.equals(that.nomUtil) : that.nomUtil != null) return false;
        if (motPasse != null ? !motPasse.equals(that.motPasse) : that.motPasse != null) return false;
        if (salt != null ? !salt.equals(that.salt) : that.salt != null) return false;
        if (role != null ? !role.equals(that.role) : that.role != null) return false;
        if (email != null ? !email.equals(that.email) : that.email != null) return false;
        if (surname != null ? !surname.equals(that.surname) : that.surname != null) return false;
        if (forename != null ? !forename.equals(that.forename) : that.forename != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = numUtil;
        result = 31 * result   (nomUtil != null ? nomUtil.hashCode() : 0);
        result = 31 * result   (motPasse != null ? motPasse.hashCode() : 0);
        result = 31 * result   (salt != null ? salt.hashCode() : 0);
        result = 31 * result   (role != null ? role.hashCode() : 0);
        result = 31 * result   (email != null ? email.hashCode() : 0);
        result = 31 * result   (surname != null ? surname.hashCode() : 0);
        result = 31 * result   (forename != null ? forename.hashCode() : 0);
        return result;
    }
}

Knowing that in my servlet-context.xml I have :

<context:component-scan base-package="com.epul" />

and all the files I gave are in this package.

Thanks in advance to you :)

CodePudding user response:

Get rid of stereotype annotations from interfaces - these are not needed at all. That is probably the root cause of your issues.

CodePudding user response:

Entity's @Id field is int but repository has Long. They have to be same. Change entity's id to Long or change repository definition to int.

@Entity
@Table(name = "utilisateur", schema = "projetpermis1", catalog = "")
public class UtilisateurEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "NumUtil", nullable = false)
    private Long numUtil;
    ...
}
  • Related