K. Siva Prasad Reddy
K. Siva Prasad Reddy

Reputation: 12405

Unable to use Spring Data MongoDB + Spring DataJPA together with SpringBoot

When I try to use Spring Data JPA and Spring Data MongoDB together with SpringBoot it is assuming my Mongo Entities also JPA entities and throwing errors.

I have Person JPA entity and PersonRepository which extends JpaRepository. And I have User Mongo Entity and UserRepository which extends MongoRepository.

When I start the application I am getting the following error:

Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.sivalabs.app.mongoentities.User
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:149)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:88)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:68)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 66 more

Is there any work around for this?

Upvotes: 3

Views: 3792

Answers (1)

Andy Wilkinson
Andy Wilkinson

Reputation: 116191

You should put your Mongo and JPA entities in separate packages (it looks like you've already done that) and then use @EnableMongoRepositories(basePackageClasses=SomeMongoEntity.class) and @EnableJpaRepositories(basePackageClasses=SomeJpaEntity.class) so that Spring Data knows where to find the Mongo and JPA entities respectively. You can see these annotations in action in Spring Boot's MixedMongoRepositoriesAutoConfigurationTests.

Upvotes: 4

Related Questions