更改Spring Boot使用的数据库模式

时间:2014-06-18 06:41:21

标签: java spring spring-boot

如何指定Spring Boot使用的数据库模式?我使用默认的hibernate(= default)和postgres(但我希望有一个通用的解决方案)。我知道如何指定JDBC URL:

spring.datasource.url=jdbc:postgresql:db_name

但遗憾的是postgresql不允许在JDBC URL中指定模式。我知道有hibernate属性hibernate.default_schema,所以我希望以下属性之一可以工作:

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

但不幸的是,他们似乎都没有任何结果。

3 个答案:

答案 0 :(得分:66)

使用spring.jpa.properties.hibernate.default_schema=schema

从Spring Boot参考指南:

  

当创建本地EntityManagerFactory时,spring.jpa.properties。*中的所有属性都作为普通的JPA属性(带有前缀剥离)传递

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

有关可用属性的完整列表,请参阅http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties

答案 1 :(得分:7)

取决于DataSource实现,必须使用哪个属性来设置默认架构(reference)。例如,对于HikariDataSource,spring.jpa.properties.hibernate.default_schema被忽略,您必须进行设置

spring.datasource.hikari.schema=schema

请参阅HikariCP配置参数here的完整列表。

答案 2 :(得分:0)

spring:
  jpa:
    properties:
      hibernate:
        default_schema: your_schema_name