Spring Data JPA存储库,替换本机查询中的架构

时间:2019-03-31 17:38:32

标签: hibernate spring-data-jpa

我需要进行一些旧代码重构。它使用多个ORACLE模式架构。当前,代码中的每个程序包都将其SQL查询作为具有已定义架构名称的本机查询运行。看起来像:

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> {
    @Query(value = "select * from AAA.Customer c where c.lastName = ?1", nativeQuery = true)
    List<Customer> findByLastName(String lastName);

@Repository
public interface ProductRepository extends CrudRepository<Product, Long> {
    @Query(value = "select * from BBB.Product p where p.id = ?1", nativeQuery = true)
    Product findById(Long id);

在公司仅使用一个 production 环境之前,它一直运行良好。现在他们想在同一台服务器上创建一个 test 环境(因此他们不能保留相同的架构名称)。

我的问题是:有什么方法可以在运行时替换架构名称(例如,应用一些前缀)?

我尝试使用“ hibernate.physical_naming_strategy”并覆盖“ toPhysicalCatalogName”和“ toPhysicalSchemaName”。不幸的是,它不适用于NativeQuery(但与JPQL命名查询完美配合)。

我尝试过的另一种方法是使用{h-schema}占位符和“ hibernate.default_schema”属性。它可以与NativeQueries一起使用,但是我只知道两个占位符(h模式和h目录)。而且我需要十多个来替换所有架构名称...

对于任何解决方法,我将感激不尽。

0 个答案:

没有答案