我用Java 17在SpringBoot 3.0.5中配置了一个MultiDataSource。主数据源工作正常,但我面临着辅助数据源的问题。
OrderRepository接口中的findAll()方法工作正常,但我在OrderQueryDsl的getOrderInfo()方法中不断收到错误。我已经试着解决这个问题好几天了,但我就是找不到原因。请帮帮我
@Configuration
@RequiredArgsConstructor
@EnableJpaRepositories(
basePackages = "com.demo.solution.invoice.repository.secondary",
entityManagerFactoryRef = "secondaryEntityManager",
transactionManagerRef = "secondaryTransactionManager"
)
public class SecondaryDataSourceConfiguration {
private final Environment env;
private final SecondaryDBPropertiesConfig secondaryDBPropertiesConfig;
@Bean
public LocalContainerEntityManagerFactoryBean secondaryEntityManager(){
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(secondaryDataSource());
em.setPackagesToScan("com.demo.solution.invoice.entity.secondary");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaPropertyMap(jpaProperties());
return em;
}
@Bean
public DataSource secondaryDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(secondaryDBPropertiesConfig.getJdbcUrl());
dataSource.setUsername(secondaryDBPropertiesConfig.getAdminID());
dataSource.setPassword(secondaryDBPropertiesConfig.getAdminPW());
return dataSource;
}
@Bean
public PlatformTransactionManager secondaryTransactionManager() {
JpaTransactionManager transactionManager= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(secondaryEntityManager().getObject());
return transactionManager;
}
private HashMap<String, String> jpaProperties(){
HashMap<String, String> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("jpa.hibernate.ddl-auto"));
properties.put("hibernate.dialect", secondaryDBPropertiesConfig.getDialect());
//properties.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
properties.put("hibernate.show_sql", env.getProperty("jpa.hibernate.show_sql"));
properties.put("hibernate.format_sql", env.getProperty("jpa.hibernate.format_sql"));
return properties;
}
}
package com.demo.solution.invoice.entity.secondary;
@NoArgsConstructor
@Entity
@Table(name = "orders")
@Getter
@ToString
public class OrderInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_id")
private Integer orderId;
@Column(name = "order_no")
private String orderNo;
@Column(name = "created_time")
private LocalDateTime createdTime;
}
package com.demo.solution.invoice.repository.secondary;
@Repository
@RequiredArgsConstructor
public class OrderQueryDsl {
private final JPAQueryFactory queryFactory;
public List\<OrderInfo\> getOrderInfo(){
List\<OrderInfo\> orderInfos = queryFactory
.selectFrom(QOrderInfo.orderInfo)
.fetch(); // error
return orderInfos;
}
}
@Repository public interface OrderRepository extends JpaRepository\<OrderInfo, String\> { }
@SpringBootTest
@ActiveProfiles("local")
class OrderQueryDslTest {
@Autowired
OrderQueryDsl orderQueryDsl;
@Test
void getOrders() {
List<OrderInfo> orderInfos = orderQueryDsl.getOrderInfo(); // error
}
}
[Full堆栈跟踪]
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.query.sqm.UnknownEntityException: Could not resolve root entity 'OrderInfo'
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:234)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:550)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:702)
at com.demo.solution.invoice.repository.secondary.OrderQueryDsl$$SpringCGLIB$$0.getOrderInfo(\<generated\>)
at
我已经尽我所能地询问了ChatGPT,但它的回答仅限于建议使用“@EntityScan”或检查“OrderInfo”类的包位置(setPackagesToScan)。它主要集中在确认“@Entity”注解是否正确应用。
1条答案
按热度按时间3bygqnnd1#
次要数据源不工作的原因是,第一个数据源在EntityManager中注入。
在EntityManager中设置PersistenceUnitName后,请创建配置文件。
您必须创建与您使用的数据库一样多的EntityManagers和JPAQueryFactory。
并使用构造函数注入和@Qualifier。
毕竟,再试一次。