Home:ALL Converter>How to set default schema name for Spring Data JPA queries?

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

Ask Time:2019-12-19T18:10:51         Author:hellzone

Json Formatter

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{
    ....
}

Author:hellzone,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59407454/how-to-set-default-schema-name-for-spring-data-jpa-queries
Orçun Çolak :

if you are using application.properties\n\nspring.datasource.url=jdbc:postgresql://myserver:5432/mydb\nspring.jpa.properties.hibernate.default_schema=myschema\nspring.datasource.username=myuser\nspring.datasource.password=mypassword\nspring.datasource.driver-class-name=org.postgresql.Driver\n",
2019-12-19T10:54:00
yy