簡體   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