Home > OS >  Why do I get the "Not a managed type" error?
Why do I get the "Not a managed type" error?

Time:08-17

My main project (A) contains the structure: x.y.z

My project depends on a jar file (B) with a.b.c package structure

Project B contains the entity that I need in project A.

In project A, I specify the following.

@EnableScheduling
@ComponentScan(
    basePackages = {"x.y.z", "a.b.c"},
    includeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Indexed.class})
    })
@EntityScan(basePackages = {"x.y.z", "a.b.c"})
@SpringBootApplication
public class Application extends SpringBootServletInitializer { ...

My entity in Project B: a.b.c.models.MyEntity;

import javax.persistence.*;

@Entity
@Table(
    name = "my_entity"
)
@EntityListeners({AuditingEntityListener.class})
public class MyEntity { ...

But every time I start, I get the same error "Not a managed type: a.b.c.models.MyEntity".

I have tried different configurations, specify different paths, use import annotation, but nothing works.

@Configuration
@Import({MyEntity.class})
... Not Work ...
@EntityScan(basePackages = {"x.y.z.*", "a.b.c.*"})
... Not Work ...
@EntityScan(basePackages = {"x.y.z.models.*", "a.b.c.models*"})
... Not Work ...    
@ComponentScan(
    basePackages = {"x.y.z.*", "a.b.c.*"},
    includeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Indexed.class})
    })

It's always the same mistake. But dependencies from Project A are loaded without problems.

I also have other projects C D E, which also depend on jar project B, it works fine in them with the same configuration.

CodePudding user response:

So. The problem was this dependency.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-indexer</artifactId>
    <version>5.3.21</version>
</dependency>

The beans in project B (a.b.c package) simply did not get into the META-INF/spring.components file.

You either need to remove this dependency, or generate your own META-INF/spring.components file for project B.

  • Related