我试图通过Springboot和JPA使用多个数据源,但是在尝试启动服务器时遇到错误。
仅当我尝试使用第二个数据源时,才会出现此问题。尝试启动我的应用程序时出现以下错误:
Not an managed type: class com.company.app.backoffice.modelDocument.Category
对于第一个数据源,一切都很好。但是,似乎我的第二个实体经理并没有跟踪好的软件包。例如,我需要第一个数据源来管理我的模型包,第二个数据源来管理modelDocument包:
<!-- Configure the data source bean -->
<!-- Website datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Doc base datasource -->
<bean id="docDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.doc.url}"/>
<property name="username" value="${jdbc.doc.username}"/>
<property name="password" value="${jdbc.doc.password}"/>
</bean>
<!-- Configure the entity manager factory bean -->
<!-- Website Entity manager -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.company.app.backoffice.model"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Doc base Entity manager -->
<bean id="docEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="docDataSource"/>
<property name="packagesToScan" value="com.company.app.backoffice.modelDocument"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Configure the transaction manager bean -->
<!-- Website transation manager -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Doc base transaction manager -->
<bean id="docTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="docEntityManagerFactory"/>
</bean>
这是我需要在第二个数据源(在modelDocument包中)管理的类:
@Entity
@Table(name = "category")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
public Long getId() {
return id;
}
public String getName() {
return name;
}
}
有人知道这是怎么回事吗?
编辑:隐式存储库来管理实体持久性
package com.company.app.backoffice.repository;
import com.company.app.backoffice.modelDocument.Category;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
编辑2:注入存储库的控制器:
@Controller
public class CategoryController {
@Autowired
private CategoryRepository categoryRepository;
@RequestMapping(value = "/categories", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public List<Category> categories() {
return categoryRepository.findAll();
}
}
最佳答案
添加包含实体的软件包扫描
<jpa:repositories base-package="your.package.enties" />