我想知道一种配置方法,它会自动将模式与其各自数据源中的实体连接起来,以便所有以前的命名查询方法也可以与 nativeQuery 方法一起正常工作。
所以,基本上我有一个应用程序,它早先连接到单个 Postgres 数据源,该数据源由 Spring-Boot 使用 applicaiton-local.properties 文件中的详细信息自动配置
现在我正在尝试连接到多个 Postgres 数据源,每个数据源都包含多个模式与我的 Spring Boot 应用程序。
一切正常,期望对于 JPA 查询我必须在每个查询中每次都明确提及模式名称。这是一个现有项目,当只有一个数据库连接和单一模式时,我已经有很多使用 Spring Jpa 进行的命名查询。 现在我将不得不手动更改很多这些查询。
以下是我的 DB1 的配置类。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"com.demo.db1.repository.schema1", "com.demo.db1.repository.schema2"}
entityManagerFactoryRef = "db1EntityManagerFactory",
transactionManagerRef = "db1TransactionManager"
)
@Slf4j
public class Db1Config {
// this takes all the DB1 config details such as URL, username & password from application-local.properties file
@Bean @ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSourceProperties db1DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "db1DataSource") @ConfigurationProperties("spring.datasource.hikari")
public DataSource db1DataSource() {
return db1DataSourceProperties().initializeDataSourceBuilder().build();
}
@Bean(name = "db1EntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean db1EntityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerBean = new LocalContainerEntityManagerFactoryBean();
entityManagerBean.setDataSource(db1DataSource());
entityManagerBean.setPackagesToScan("com.demo.db1.entity.schema1",
"com.demo.db1.entity.schema2");
entityManagerBean.setJpaProperties(additionalProperties());
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerBean.setJpaVendorAdapter(vendorAdapter);
return entityManagerBean;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("spring.jpa.show-sql", "true");
return properties;
}
@Bean(name = "db1TransactionManager") @Primary
public PlatformTransactionManager db1TransactionManager() {
return new JpaTransactionManager(db1EntityManagerFactory().getObject());
}
@Bean
@Primary
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
DB2 的另一个完全相同的类也有不同的参数,分别用于 DB2。
以下是一个实体类的例子:
@Entity
@Table(name = "user_loan" , schema = "schema_name")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class LoanEntity extends BaseEntity { //Base entity contains the basic columns like Id and such.
@Column(name = "account_type_id")
private Integer accountTypeId;
@Column(name = "user_id")
private String userId;
private String userName;
private String userBusinessName;
private String userMobile;
private String userEmail;
每个实体都存在类似的实体类,分别基于它们的数据库和架构
以下是回购类的示例:
@Repository
public interface ILoanRepository extends JpaRepository<LoanEntity, Long>,
JpaSpecificationExecutor<LoanEntity>, PagingAndSortingRepository<LoanEntity, Long> {
// the following query works fine
@Query(value = "select * from schema_name.user_loan where order_int_id = :orderIntId " +
" and user_id = :userId ", nativeQuery = true)
LoanEntity findByOrderIntIdAndUserId(Integer orderIntId, String userId);
// but in case of any of the following variation of the query, it is either giving error or wrong result.
1. LoanEntity findByOrderIntIdAndUserId(Integer orderIntId, String userId); // simple auto-generated Jpa named query.
2. @Query(value = "select * from user_loan where order_int_id = :orderIntId and user_id = :userId ", nativeQuery = true)
LoanEntity findByOrderIntIdAndUserId(Integer orderIntId, String userId); // query without schema specified in the table_name
3. @Query(value = "select l from LoanEntity where l.orderIntId = ?1 and l.userId = ?2")
LoanEntity findByOrderIntIdAndUserId(Integer orderIntId, String userId); // in such query I'm also unable to define the schema in the query itself.
我有很多 1、2 和 3 类型的查询,我不想修改所有这些。
有人可以帮忙吗??我想配置这样我就不必手动修改我的应用程序中所有现有的查询方法。