Home > database >  Error during startup of Spring Boot JPA app with error that entity class is not a manged type
Error during startup of Spring Boot JPA app with error that entity class is not a manged type

Time:12-01

First I am quite new to Springboot, learning from examples, but I am struggling with a real simple one.

I have a very minimal Springboot App with 1 Main Class, 1 repository, 1 entity. The database is PosgreSQL.

When starting the application the output contains the error message

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cityRepository' defined in com.d043114.minimalJPA.CityRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.d043114.minimalJPA.City

The repository looks like this

package com.d043114.minimalJPA;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CityRepository extends CrudRepository<City, Long> {

}

The entity is like this:

package com.d043114.minimalJPA;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "cities")
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO )
    private long ID;
    
    public long getID() {
        return ID;
    }

    public void setID(long iD) {
        ID = iD;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;

}

The main class is basic as well

package com.d043114.minimalJPA;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;


@ComponentScan(basePackages = "com.d043114.minimalJPA")
@SpringBootApplication
@EntityScan("com.d043114.minimlaJPA")
public class MinimalJpaApplication {

    public static void main(String\[\] args) {
        SpringApplication.run(MinimalJpaApplication.class, args);
    }

}


I played around with the @EntityScan and @ComponentScan, but am suprised that it is not working, even though repository, main class and entity are under the same package.

CodePudding user response:

Try adding this annotation as well, over 'MinimalJpaApplication' class:

@EnableJpaRepositories("my.package.base.*")
  • Related