gpt4 book ai didi

java - 如何为 Spring Data JPA 查询设置默认架构名称?

转载 作者:行者123 更新时间:2023-11-30 05:22:00 24 4
gpt4 key购买 nike

我想为 postgresql 设置默认架构。当我尝试设置属性(“hibernate.default_schema”)并使用 schema 属性更新表注释时,hibernate 仍然会生成没有任何架构名称的查询,如下所示;

select log0_.id as id1_0_ from log log0_

当我将表名更改为“dbo.log”时,我可以获得结果集,没有任何错误。

 @Table(name = "dbo.log")

有没有办法为 postgresql 查询设置默认模式?

@Configuration
@EnableJpaRepositories(
basePackages = "com.test.repository.postgresql",
entityManagerFactoryRef = "postgresqlEntityManagerFactory",
transactionManagerRef = "postgresqlTransactionManager"
)
public class PostgreSQLConfig {

....

@Bean(name = "postgresqlEntityManagerFactory")
public EntityManagerFactory entityManagerFactory(@Qualifier("postgresqlDatasource") DataSource postgresqlDatasource) {

Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.dialect", hibernateDialect);
jpaProperties.setProperty("hibernate.show_sql", hibernateShowSql);
jpaProperties.setProperty("hibernate.ddl-auto", hibernateDDLAuto);
jpaProperties.setProperty("hibernate.naming.physical-strategy", hibernatePhysicalStrategy);
jpaProperties.setProperty("hibernate.default_schema", "dbo");

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setJpaProperties(jpaProperties);
factory.setPackagesToScan("com.test.entity.postgresql");
factory.setDataSource(postgresqlDatasource);
factory.afterPropertiesSet();
return factory.getObject();
}

}

这是我的实体类;

@Entity
@Table(name = "log", schema = "dbo")
public class Log{
....
}

最佳答案

如果您正在使用application.properties

spring.datasource.url=jdbc:postgresql://myserver:5432/mydb

spring.jpa.properties.hibernate.default_schema=myschema

spring.datasource.username=myuser

spring.datasource.password=mypassword

spring.datasource.driver-class-name=org.postgresql.Driver

关于java - 如何为 Spring Data JPA 查询设置默认架构名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59407454/

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