Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change database schema used by Spring Boot

How do I specify database schema used by Spring Boot? I am using default hibernate (=default) and postgres (but i hoping for a generic solution). I know how to specify JDBC URL:

spring.datasource.url=jdbc:postgresql:db_name 

But unfortunately postgresql does not allow to specify schema in JDBC URL. I know that there is hibernate property hibernate.default_schema, so I was hoping that one of the following properties will work:

hibernate.default_schema=schema spring.hibernate.default_schema=schema spring.jpa.hibernate.default_schema=raw_page 

But unfortunately neither of them seems to have any result.

like image 759
Paperback Writer Avatar asked Jun 18 '14 06:06

Paperback Writer


People also ask

How do you set a schema in DataSource?

there is added support for specifying the current schema using URL. With @ClassRule, we create an instance of PostgreSQL database container. Next, in the setup method, create a connection to that database and create the required objects. To change the default schema, we need to specify the currentSchema parameter.

How do I connect multiple schemas in spring boot?

Until now with spring 4 and XML configuration I was able to only put the DB URL like: jdbc:mysql://180.179.57.114:3306/?zeroDateTimeBehavior=convertToNull and in the entity class specify the schema to use and thus able to connect to multiple schemas.

What is @schema in spring boot?

Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema. sql and data. sql , respectively. In addition, Spring Boot processes the schema-${platform}.


2 Answers

Use for application.properties:

spring.jpa.properties.hibernate.default_schema=your_scheme  

OR for application.yaml:

spring:   jpa:     properties:       hibernate.default_schema: your_scheme 

From the Spring Boot reference guide:

all properties in spring.jpa.properties.* are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory is created

See http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties

For a full list of available properties see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties

like image 110
M. Deinum Avatar answered Sep 29 '22 06:09

M. Deinum


It depends on the DataSource implementation which property has to be used to set the default schema (reference). With HikariDataSource for example spring.jpa.properties.hibernate.default_schema is ignored and you have to set

spring.datasource.hikari.schema=schema 

See the complete list of HikariCP configuration parameters here.

like image 24
vboerchers Avatar answered Sep 29 '22 06:09

vboerchers