简体   繁体   English

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

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

I want to set default schema for postgresql.我想为 postgresql 设置默认架构。 When I try to set property("hibernate.default_schema") and also update table anotation with schema prop, hibernate still generates query without any schema name like below;当我尝试设置 property("hibernate.default_schema") 并使用 schema prop 更新表注释时,hibernate 仍然生成没有任何架构名称的查询,如下所示;

select log0_.id as id1_0_ from log log0_

When I change table name to "dbo.log", I can get resultSet without any error.当我将表名更改为“dbo.log”时,我可以毫无错误地获得 resultSet。

 @Table(name = "dbo.log")

Is there any way to set default schema for postgresql queries?有没有办法为 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();
    }

}

Here is my entity class;这是我的实体类;

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

if you are using application.properties如果您正在使用 application.properties

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

spring.jpa.properties.hibernate.default_schema=myschema spring.jpa.properties.hibernate.default_schema=myschema

spring.datasource.username=myuser spring.datasource.username=myuser

spring.datasource.password=mypassword spring.datasource.password=我的密码

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

That should be declard in the JDBC Connection URL in the DataSource .这应该在DataSource的 JDBC Connection URL 中声明。 The URL schema looks like: jdbc:postgresql://host:port/database , where database is the schema. URL 模式看起来像: jdbc:postgresql://host:port/database ,其中 database 是模式。

If that's not enough set also in the URL the currentSchema .如果这还不够,请在 URL 中设置currentSchema

Specify the schema (or several schema separated by commas) to be set in the search-path.指定要在搜索路径中设置的架构(或多个以逗号分隔的架构)。 This schema will be used to resolve unqualified object names used in statements over this connection.此模式将用于解析在此连接上的语句中使用的非限定对象名称。

Here are the docs for the JDBC Driver: https://jdbc.postgresql.org/documentation/head/connect.html以下是 JDBC 驱动程序的文档: https : //jdbc.postgresql.org/documentation/head/connect.html

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

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