Spring JPA 无法扫描多JAR包中@Entity类的问题
问题描述:项目中使用Maven管理,将项目划分为多个子模块,每个子模块中都自己模块的实体类。 在主模块中使用Spring JPA的配置如下:
<bean id="entityManagerFactory" ref="dataSource"/> <property name="persistenceUnitName" value="persistence"/> <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> <property name="jpaProperties"> <props> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.SingletonEhCacheRegionFactory</prop> <prop key="net.sf.ehcache.configurationResourceName">ehcache/ehcache-hibernate-local.xml</prop> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> </props> </property></bean>
1. 删除对应的entityManagerFactory中的属性: <property name="persistenceUnitName" value="persistence"/>;2. 增加新的entityManagerFactory属性: <property name="packagesToScan" value="自己的Entity类包名"/>3. 删除persistence对应的配置文件;