当我尝试将Spring Data JPA和Spring Data MongoDB与SpringBoot一起使用时,它假设我的Mongo实体也是JPA实体并抛出错误。
我有Person JPA实体和PersonRepository,它扩展了JpaRepository。我有User Mongo Entity和UserRepository,它扩展了MongoRepository。
当我启动应用程序时,我收到以下错误:
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
有没有解决这个问题?
答案 0 :(得分:4)
您应该将Mongo和JPA实体放在单独的包中(看起来您已经这样做了),然后使用@EnableMongoRepositories(basePackageClasses=SomeMongoEntity.class)
和@EnableJpaRepositories(basePackageClasses=SomeJpaEntity.class)
,以便Spring Data知道在哪里找到分别是Mongo和JPA实体。您可以在Spring Boot的MixedMongoRepositoriesAutoConfigurationTests
中看到这些注释。