Have anyone got this issue resolved? "Not a managed type".

Sounds quite a simple one, but annoying that it is already defined inside the scanning package hierarchy.

Details of what I'm trying to accomplish are as follows:

Aim

I'm trying to get Unit Tests (JUnit5) for JPA running in my demo project. Here's the code:

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

com.example.demo.DemoApplication:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan("com.example.demo")

public class DemoApplication {

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

}

com.example.demo.domain.MyObj:

package com.example.demo.domain;

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

@Entity
public class MyObj {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String message;
}

com.example.demo.domain.MyObjRepository:

package com.example.demo.domain;

import org.springframework.data.jpa.repository.JpaRepository;

//@Repository
public interface MyObjRepository extends JpaRepository<MyObj, Long> {
}

com.example.demo.DemoApplicationTests:

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}

Issue

When the tests are run mvn clean test I get this error which stops the build/test:


[ERROR] com.example.demo.DemoApplicationTests.contextLoads -- Time elapsed: 0.004 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@16391278 testClass = com.example.demo.DemoApplicationTests, locations = [], classes = [com.example.demo.DemoApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1b11171f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@4b0d79fc, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@1ed6388a, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@55a147cc, org.springframework.boot.test.context.SpringBootTestAnnotation@129aca4d], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:180)
.
.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myObjRepository' defined in com.example.demo.domain.MyObjRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.demo.domain.MyObj
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1775)
.
.


Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.demo.domain.MyObj
    at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:193)
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:463)
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:97)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:82)


What I did so far

I tried the following threads with no luck:

Spring-boot : Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.smart.entities.User - Stack Overflow Spring-boot : Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.smart.entities.User

hibernate - Error creating bean with name 'XXX: Unsatisfied dependency expressed through field 'XXX' - Stack Overflow Error creating bean with name 'XXX: Unsatisfied dependency expressed through field 'XXX'

1

There are 1 best solutions below

0
MaduKan On

So -- the issue was that I was using javax.persistence.

Changed to jakarta.persistence and problem went away.

I quote form:

java - Spring boot not a managed type - Stack Overflow Spring boot not a managed type

"Judging from your stacktrace you are using SPring Boot 3. Judging from your entity you are messing around with your dependencies and included incompatible ones. The annotations should come from jakarta.persistence package not javax.persistence. EIther fix the annotations or downgrade Spring Boot to 2.7.5. – M. Deinum Nov 29, 2022 at 15:29"