gpt4 book ai didi

java - 使用 JPA 时多个数据源抛出错误

转载 作者:行者123 更新时间:2023-12-02 03:11:31 24 4
gpt4 key购买 nike

我尝试使用 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:注入(inject)存储库的 Controller :

@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" />

关于java - 使用 JPA 时多个数据源抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40972464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com