【问题标题】:jdbcTemplate unable to resolve {h-schema} on runtimejdbcTemplate 无法在运行时解析 {h-schema}
【发布时间】:2020-01-25 20:32:43
【问题描述】:

我正在尝试使用 spring.jpa.properties.hibernate.default_schema 的 spring 属性在运行时获取数据库模式名称,但它没有在运行时解析 {h-schema}。

DemoApplication.java

public class DemoApplication 
{
    public static void main(String[] args) 
    {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        Test test = context.getBean(Test.class);
        System.err.println("The required count is = "+test.getCount());
    }
}

Test.java

@FunctionalInterface
public interface Test 
{
    public BigDecimal getCount();
}

TestImpl.java

@Service
public class TestImpl implements Test
{

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public BigDecimal getCount() 
    {        
        return (BigDecimal) jdbcTemplate.queryForList("select count(*) from {h-schema}test").get(0).values().toArray()[0];
    }

}

application.properties

spring.datasource.url=jdbc:oracle:thin:@XXX:YYY/ZZZ
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.username=X1
spring.datasource.password=Y1
spring.jpa.properties.hibernate.default_schema=Z1

#hibernate config
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.hibernate.ddl-auto=none

logging.level.org.springframework.jdbc.core = TRACE

错误日志

Executing SQL query [select count(*) from {h-schema}test]

Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [select count(*) from {h-schema}test]; SQL state [99999]; error code [17034]; Non supported SQL92 token at position: 22; nested exception is java.sql.SQLException: Non supported SQL92 token at position: 22

我不确定如何将属性文件中的 spring.jpa.properties.hibernate.default_schema 变量解析为 {h-schema}。请为我提供任何适当的方法或替代问题的方法。

[Edit] : 经过大量调试,我发现 spring 提供了两个属性,即 spring.datasource.hikari.schemaspring.datasource.hikari.connection-init-sql 这可能满足某些需求,但它们也会改变会话,这意味着我无法从我登录的模式中查询数据,因此我仍然希望有人提出与解决 {h-schema} 概念相关的建议来自属性文件,这样我就可以在本机查询中根据需要切换架构,而无需硬编码架构名称或显式更改架构会话。

【问题讨论】:

    标签: java hibernate spring-boot oracle11g


    【解决方案1】:

    简而言之;由于在您的 application.properties(或 .yml)文件中声明了默认架构信息,您可以通过 @Value 注释获取此值并可以使用它在你的 SQL 语句中。

    对于 "show me the code" 类,你的 TestImpl.java 类应如下所示;

    @Service
    public class TestImpl implements Test
    {
    
        @Autowired
        JdbcTemplate jdbcTemplate;
    
        @Value("${spring.jpa.properties.hibernate.default_schema}")
        private String defaultSchema;
    
        @Override
        public BigDecimal getCount() 
        {        
            return (BigDecimal) jdbcTemplate.queryForList("select count(*) from  " + defaultSchema + ".test").get(0).values().toArray()[0];
        }
    
    }
    

    注意:不要错过添加“。”在你的表名之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-05
      • 2015-05-22
      • 1970-01-01
      • 2012-11-14
      • 2017-03-30
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多