【发布时间】: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.schema 和 spring.datasource.hikari.connection-init-sql 这可能满足某些需求,但它们也会改变会话,这意味着我无法从我登录的模式中查询数据,因此我仍然希望有人提出与解决 {h-schema} 概念相关的建议来自属性文件,这样我就可以在本机查询中根据需要切换架构,而无需硬编码架构名称或显式更改架构会话。
【问题讨论】:
标签: java hibernate spring-boot oracle11g