【问题标题】:Change database schema used by Spring Boot更改 Spring Boot 使用的数据库模式
【发布时间】:2014-08-08 08:12:49
【问题描述】:

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

spring.datasource.url=jdbc:postgresql:db_name

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

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

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

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    用于application.properties

    spring.jpa.properties.hibernate.default_schema=your_scheme 
    

    application.yaml

    spring:
      jpa:
        properties:
          hibernate.default_schema: your_scheme
    

    来自 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

    【讨论】:

    • 看来我错过了指南中的这一部分。我不确定为什么 ddl-auto 被 spring.jpa.hibernate.ddl-auto 更改,但架构被“spring.jpa.properties.hibernate.default_schema”更改。它仍然按照指南中的说明工作,非常感谢!
    • spring.jpa.hibernate.xspring.jpa.properties.hibernate.x 确实令人困惑。 spring.jpa.properties.hibernate. 为我工作。
    • 注意schema名称必须小写
    • 可用属性的完整列表是here
    【解决方案2】:

    这取决于 DataSource 实现必须使用哪个属性来设置默认模式 (reference)。以 HikariDataSource 为例,spring.jpa.properties.hibernate.default_schema 被忽略,您必须设置

    spring.datasource.hikari.schema=schema
    

    查看 HikariCP 配置参数的完整列表here

    【讨论】:

    • 确实,我设置了 ikari 数据源,它无法通过 spring.jpa.properties.hibernate.default_schema 获取架构,但它在 ikari 方式下运行良好。
    【解决方案3】:
    spring:
      jpa:
        properties:
          hibernate:
            default_schema: your_schema_name
    

    【讨论】:

      【解决方案4】:

      spring.jpa.properties.hibernate.default_schema=your_scheme

      弹簧: jpa: 特性: hibernate.default_schema: your_scheme

      使用 HikariDataSource 例如 spring.jpa.properties.hibernate.default_schema 被忽略,你也必须设置

      spring.datasource.hikari.schema=your_scheme

      【讨论】:

      • spring.datasource.hikari.schema=your_scheme,就我而言,这也不起作用。我在 application.properties 中添加了这个仍然遇到错误。 PSQLException:错误:关系“result_details”不存在位置:16 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
      【解决方案5】:

      我遇到了错误: 无法从数据源获取连接 org.postgresql.util.PSQLException:错误:不支持的启动参数:search_path

      解决方案: 应用程序-xyz_dev.yml

      网址:jdbc:postgresql://localhost:8080/your_database?search_path=your_scheme&stringtype=unspecified

      弹簧: jpa: 特性: hibernate.default_schema: your_scheme

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-28
        • 1970-01-01
        • 2023-03-16
        • 2018-01-11
        • 1970-01-01
        • 2020-07-27
        • 2017-04-06
        相关资源
        最近更新 更多