Not a managed type entity spring boot

While deploying the Spring Boot application we may encounter a nested exception is java.lang.IllegalArgumentException: Not a managed type. In this quick post, we will see how to fix the not a managed type entity spring boot error.

Spring Data JPA Interview Questions and Answers
How to write custom method in the repository in Spring Data JPA

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.javatute.entity.Student

Fix for Not a managed type error.

Use @EntityScan annotation in the main class

The directory structure for the entity.

Not a managed type entity spring boot
Not a managed type entity spring boot
@ComponentScan(basePackages = "com.javatute")
@SpringBootApplication
@EntityScan("com.javatute.entity")
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);
    }
}

The below code will also work fine.

@ComponentScan(basePackages = "com.javatute")
@SpringBootApplication
@EntityScan("com.javatute.*")
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);
    }
}

But the below code will not work.

@ComponentScan(basePackages = "com.javatute")
@SpringBootApplication
@EntityScan("com.javatute.entity.*")
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);
    }
}

Note – Make sure we are using org.springframework.boot.autoconfigure.domain.EntityScan class.

The main reason for Not a managed type exception is, our entities classes are not scanned. Observed the below code snippet from where we are getting this exception.

	public <X> ManagedTypeDescriptor<X> managedType(Class<X> cls) {
		ManagedType<?> type = jpaEntityTypeMap.get( cls );
		if ( type == null ) {
			type = jpaMappedSuperclassTypeMap.get( cls );
		}
		//some more code
		if ( type == null ) {
			throw new IllegalArgumentException( "Not a managed type: " + cls );
		}
		return (ManagedTypeDescriptor<X>) type;
	}

How to fix not a managed type error without using @EntityScan annotation.

We can configure LocalContainerEntityManagerFactoryBean manually and use setPackagesToScan() method. We need to pass the entity package name as a parameter.

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setDataSource(dataSource);
        bean.setPackagesToScan("com.javatute.entity");
        return bean;
    }

Additional note – Still if the issue is not resolved, use @ComponentScan, @EntityScan, and @EnableJpaRepositories annotations properly. Follow this tutorial that explains how to use these annotations.

Note – We can use the below configuration for hibernate-based configuration.

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.javatute.entity" />
   
</bean>

Stacktrace of this exception.

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.javatute.entity.Student
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:582) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.(JpaMetamodelEntityInformation.java:75) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:229) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:179) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:162) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:72) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:309) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]

That’s all about how to fix Not a managed type entity spring boot error.

See how to fix Failed to lazily initialize a collection of role could not initialize proxy – no Session

Spring Data JPA tutorial

Hibernate tutorials.