简体   繁体   中英

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

I want to set default schema for 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;

select log0_.id as id1_0_ from log log0_

When I change table name to "dbo.log", I can get resultSet without any error.

 @Table(name = "dbo.log")

Is there any way to set default schema for postgresql queries?

@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

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

That should be declard in the JDBC Connection URL in the DataSource . The URL schema looks like: jdbc:postgresql://host:port/database , where database is the schema.

If that's not enough set also in the URL the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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