How to Set Up Default Schema Name in JPA Configuration

How to set up default schema name in JPA configuration?

Don't know of JPA property for this either. But you could just add the Hibernate property (assuming you use Hibernate as provider) as

...

<property name="hibernate.default_schema" value="myschema"/>

...

Hibernate should pick that up

Change database schema used by Spring Boot

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

Hibernate default schema and Table annotation

After a year now app evolved and I am using Spring Boot 1.5.4 and Hibernate 5.1.5 with Postgres 9.6. Not sure if there were issues with previous versions but now it works fine.

yaml configuration file:

spring:
datasource:
driver-class-name: org.postgresql.Driver
platform: postgresql
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQL94Dialect
default_schema: SCHEMA_NAME

Although using 9.6, PostgreSQL94Dialect can be used for 9.4 and later as there is no specific PostgreSQL96Dialect for given Hibernate version.

With this there is no need to specify schema in @Table annotation.

Update October 2018

See Hibernate's repository for supported dialects and set git tag to your Hibernate version: https://github.com/hibernate/hibernate-orm/tree/master/hibernate-core/src/main/java/org/hibernate/dialect

Spring JPA: Providing Schema Name Dynamically

If you need to change the schema name at the runtime, I recommend to use Hibernate multi-tenancy approach. You could find more details here and here



Related Topics



Leave a reply



Submit