5

I'm trying to build a library with spring which will handle task management for other spring boot applications. My library includes services, repositories and entities. Library will use data source of parent project for entities. My target is using task management library in other spring projects with only using @EnableTask annotation.

To do that, I have prepared my library and it works as I expected. But when I tried to import this library to a spring boot application, repositories and entities from library were not available.

My EnableTask annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(TaskConfig.class)
public @interface EnableTask {

}

And TaskConfig

@Configuration
@ComponentScan("com.cbidici.task")
public class TaskConfig {

}

To make repositories from library registred I have changed my configuration class as below.

@Configuration
@ComponentScan("com.cbidici.task")
public class TaskConfig {

    @Bean
    public TaskRepository taskRepository(EntityManager entityManager) {
        JpaRepositoryFactory jpaRepositoryFactory=new JpaRepositoryFactory(entityManager);
        return jpaRepositoryFactory.getRepository(TaskRepository.class);
    }

}

And it worked...

Now the part I mess up everything! With this configuration I've got excception that my Task entity is not a managed type.

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.cbidici.task.entity.Task
    at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:552) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:74) ~[spring-data-jpa-2.1.2.RELEASE.jar:2.1.2.RELEASE]

To make entities registered, I've tried to add EntityScan to my TaskConfig class. This messes up SpringBootApplication and my parent project does not scan entities in itself.

I've found this issue and there is a work around solution in it. https://github.com/spring-projects/spring-boot/issues/6141

But, this solution includes adding @EnableJpaRepositories, @EntityScan to my parent project which I don't want to.

I think I need to find a way to register entities in library manually as I did for repositories but I couldn't.

cbidici
  • 51
  • 3

0 Answers0