繁体   English   中英

如何为 Spring Data JPA 查询设置默认模式名称?

[英]How to set default schema name for Spring Data JPA queries?

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

select log0_.id as id1_0_ from log log0_

当我将表名更改为“dbo.log”时,我可以毫无错误地获得 resultSet。

 @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=我的密码

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

这应该在DataSource的 JDBC Connection URL 中声明。 URL 模式看起来像: jdbc:postgresql://host:port/database ,其中 database 是模式。

如果这还不够,请在 URL 中设置currentSchema

指定要在搜索路径中设置的架构(或多个以逗号分隔的架构)。 此模式将用于解析在此连接上的语句中使用的非限定对象名称。

以下是 JDBC 驱动程序的文档: https : //jdbc.postgresql.org/documentation/head/connect.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM