gpt4 book ai didi

java - Hibernate的属性hibernate.default_schema无法正常工作

转载 作者:行者123 更新时间:2023-12-02 05:39:47 25 4
gpt4 key购买 nike

我想添加在 native 查询中使用的参数化数据库名称我引用了这个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;
}

Hibernate后台生成的查询

 select * from summit

实际输出

select * from summit

预期输出

select * from student.summit

缺少数据库名称。我哪里出错了?提前谢谢您。

最佳答案

在 persistence.xml 文件中指定默认架构:

<property name="hibernate.default_schema" value="yourSchemaName" />

关于java - Hibernate的属性hibernate.default_schema无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com