【问题标题】:How to do in spring boot that connects to a scheme in a database and not to the public如何在 Spring Boot 中连接到数据库中的方案而不是公众
【发布时间】:2019-12-03 12:57:23
【问题描述】:

我有一个Spring Boot api,它连接到postgresql中的数据库,在数据库中我有两个方案,一个是我自己的,一个是公共的。 插入我在我的方案中的表时,是在公共中创建并插入那里的。 我试图在实体中输入我的方案名称,但它给了我一个错误并说它不存在,我不知道是否有必要这样做:

@Entity
@Table(  name = "rules" , schema = "eschema1")

这是我的 application.properties:

spring.datasource.url=jdbc:postgresql://15.98.0.65:5432/database
spring.datasource.username=postgres
spring.datasource.password=
spring.datasource.driver-class-name=org.postgresql.Driver


spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=none

【问题讨论】:

  • 我希望您并没有真正为您的 Web 应用程序使用 postgres 用户?
  • 是的,我们正在使用它,为什么?
  • 已经,对于某些测试来说不是最终的,现在我需要它来工作

标签: postgresql hibernate spring-boot application.properties


【解决方案1】:

我看到了三个解决方案:

  1. ?currentSchema=eschema1 添加到connection URL

  2. 永久更改该用户的search path

    alter user postgres set search_path = 'public,eschema1';
    
  3. 告诉您的混淆层 (=ORM) 为表添加您配置的架构的前缀。具体是怎么做的,我不知道。


我强烈建议您使用与postgres 不同的用户——即使它只是为了测试目的。

【讨论】:

  • 我做了第一件事,它给了我这个错误:2019-12-04 09:31:08.410 ERROR 22300 --- [nio-8080-exec-2] o.h.engine.jdbc.spi .SqlExceptionHelper:错误:关系“规则”不存在位置:162 2019-12-04 09:31:08.424 INFO 22300 --- [nio-8080-exec-2] o.h.e.internal.DefaultLoadEventListener:HHH000327:执行加载命令时出错:org.hibernate.exception.SQLGrammarException:无法提取结果集
  • 我刚刚看到它对我不起作用,因为在执行 from schema.Table 时将方案放置在不带括号的情况下,并且仅在将其放在括号中时才对我有用,就像我在 java 中那样插入时用括号括起来
猜你喜欢
  • 2017-05-06
  • 2019-02-07
  • 2018-11-08
  • 2016-12-17
  • 2019-01-13
  • 2022-08-18
  • 2018-10-10
  • 2017-11-29
  • 2019-08-10
相关资源
最近更新 更多