我想添加在本机查询中使用的参数化数据库名称 我引用此URL来实施 https://vladmihalcea.com/how-to-resolve-the-hibernate-global-database-schema-and-catalog-for-native-sql-queries/ 我已经在舞台上进行了测试并测试了环境,并且一切正常 缺少数据库名称。 下面是我的代码
application.yml
hibernate:
show_sql: true
format_sql: true
dialect: org.hibernate.dialect.MySQLDialect
hbm2ddl.auto: false
default_schema: student
SummitRepository.java
@Query(value = "select * from {h-schema}summit", nativeQuery = true)
public List<Summit> getActiveUpcomingSummitsForChannelIds();
TestDbConfig
public class TestDbConfig {
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter hibernateJpa = new HibernateJpaVendorAdapter();
hibernateJpa.setDatabasePlatform(env
.getRequiredProperty("hibernate.dialect"));
hibernateJpa.setShowSql(env.getRequiredProperty("hibernate.show_sql",
Boolean.class));
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(datasource());
emf.setJpaVendorAdapter(hibernateJpa);
emf.setPackagesToScan("com.xxx.student.model");
Map<String, String> jpaProps = new HashMap<String, String>();
jpaProps.put("hibernate.dialect",
env.getRequiredProperty("hibernate.dialect"));
jpaProps.put("hibernate.format_sql",
env.getRequiredProperty("hibernate.format_sql"));
jpaProps.put("hibernate.hbm2ddl.auto",
env.getRequiredProperty("hibernate.hbm2ddl.auto"));
jpaProps.put("hibernate.show_sql",
env.getRequiredProperty("hibernate.show_sql"));
***jpaProps.put("hibernate.default_schema",
env.getRequiredProperty("hibernate.default_schema"));***
emf.setJpaPropertyMap(Collections.singletonMap(
"javax.persistence.validation.mode", "none"));
return emf;
}
休眠后台生成的查询
select * from summit
实际输出
select * from summit
预期产量
select * from student.summit
缺少数据库名称。 我要去哪里错了? 谢谢你。
答案 0 :(得分:0)
在persistence.xml文件中指定默认架构:
<property name="hibernate.default_schema" value="yourSchemaName" />