0

So, I have a problem like this: 2020-01-30 22:54:20.059 ERROR 8040 --- [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dietaController': Unsatisfied dependency expressed through field 'dietaRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dietaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.diet4you.LapkoEkaterina.Entity.Dieta My pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.diet4you</groupId>
    <artifactId>LapkoEkaterina</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>

    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.16.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>
</project>

My Entity:

@Entity
@Table(name = "diety")
public class Dieta {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name="DIETA_ID") private int dietaId;
    @NotEmpty
    @Column(unique=true, name ="DIETA_NAZWA")
    private String nazwa;
    @NotEmpty
    @Column(name="OPIS") private String opis;
    public Dieta(){ }
    public Dieta (int dietaId,String nazwa, String opis )
    { this.dietaId = dietaId;
    this.nazwa = nazwa;
    this.opis = opis; }
    public int getDietaId() {
        return dietaId; }
        public void setDietaId(int dietaId) {
        this.dietaId = dietaId; }
        public String getNazwa() {
        return nazwa; }
        public void setNazwa(String nazwa) {
        this.nazwa = nazwa; }
        public String getOpis() {
        return opis; }
        public void setOpis(String opis) {
        this.opis = opis; }
}

My repository:

@Repository
public interface DietaRepository extends JpaRepository<Dieta, String> {
    Dieta findByName (String name);
}

And my Controller:

@RestController
public class DietaController {
    @Autowired
    private DietaRepository dietaRepository;
    @GetMapping("/dieta")
    public List<Dieta> getAllNotes() {
        return dietaRepository.findAll();
    }

}

Application java class:

@Configuration
@SpringBootApplication
@EnableJpaRepositories
@EntityScan ( basePackages  = { " com.diet4you.LapkoEkaterina" })
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
lapka1002
  • 5
  • 1
  • 5
  • I think the entity class is not auto detected . Pleas go through [this question](https://stackoverflow.com/questions/28664064/spring-boot-not-an-managed-type) – R.G Jan 31 '20 at 00:23
  • I saw it , it didn't help – lapka1002 Jan 31 '20 at 01:04
  • As mentioned your class doesn't see your repository. It's obvious in the error message. can you add the packages of your classes above and also the class were you have the annotation `@SpringBootApplication` – Alex P. Jan 31 '20 at 01:31
  • Also have you added something like `@EntityScan(basePackages = "com.diet4you.LapkoEkaterina.Entity.")` in your configuration class. You need to tell spring boot that this entity has to be scanned to be managed by spring – Alex P. Jan 31 '20 at 01:37
  • I added the class :) – lapka1002 Jan 31 '20 at 01:54
  • Also add the ___@ComponentScan(basePackages = “com.diet4you.LapkoEkaterina”)___. This makes spring possible to see which of your classes are annotated as components like ___@Controller___ etc. If they are in different packages you can add multiple, or one were they fall under the same hood. – Alex P. Jan 31 '20 at 02:24
  • It unchanged :( – lapka1002 Jan 31 '20 at 02:40
  • The repository appears to be wrong. `@Repository public interface DietaRepository extends JpaRepository { Dieta findByName (String name); }` There is no property by the name "name" in Dieta. Please update the repository as follows `@Repository public interface DietaRepository extends JpaRepository { Dieta findByNazwa(String nazwa); }` – R.G Jan 31 '20 at 04:34
  • @lapka1002 I already gave you answer [here](https://ru.stackoverflow.com/questions/1076501/error-creating-bean-with-name-dietacontroller?noredirect=1#comment1848925_1076501) – Dmitrii Jan 31 '20 at 04:44
  • Also , by any chance do you have two Dieta classes in your project ? – R.G Jan 31 '20 at 04:45
  • When i chanced Repository , it started working – lapka1002 Jan 31 '20 at 20:16

1 Answers1

1
  1. remove space here @EntityScan ( basePackages = { " com.diet4you.LapkoEkaterina" }) between " and com.diet4you...

  2. change extends JpaRepository<Dieta, String> to extends JpaRepository<Dieta, Integer>

  3. remove or fix Dieta findByName (String name) at repository interface, it's mistake

Dmitrii
  • 168
  • 6