postgresql Spring Boot spring.datasource.schema VS spring.jpa.properties.hibernate.default_schema

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35082968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:09:43  来源:igfitidea点击:

Spring Boot spring.datasource.schema VS spring.jpa.properties.hibernate.default_schema

hibernatepostgresqlspring-bootspring-dataspring-data-jpa

提问by Chrisma Daniel

Recently, I begin to use Spring Boot for web app development.

最近,我开始使用 Spring Boot 进行 Web 应用程序开发。

This is my .properties file content:

这是我的 .properties 文件内容:

#data source configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/sampledb
spring.datasource.schema=sample
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.minimumIdle=3
spring.datasource.maximumPoolSize=5



#jpa properties configuration
#spring.jpa.show-sql=false
spring.jpa.databasePlatform=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=validate
#spring.jpa.properties.hibernate.default_schema=sample

This part of my entity class:

我的实体类的这一部分:

@Entity
@Table(name = "sample_info")
public class SampleInfo implements Serializable{

    private Long id;
    private String code;
    private Long serialNumber;

    @Id
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "sample_info_seq_gen"
    )
    @SequenceGenerator(
            name = "sample_info_seq_gen",
            sequenceName = "sample_info_seq",
            allocationSize = 1
    )
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

Based on .properties above, the issue is every time I try to save new SampleInfo using Spring Data JPA repository, i always get error sequence "sample_info_seq" not found.

基于上面的 .properties,问题是每次我尝试使用 Spring Data JPA 存储库保存新的 SampleInfo 时,我总是找不到错误序列“sample_info_seq”。

If I comment spring.datasource.schema=sample and uncomment spring.jpa.properties.hibernate.default_schema=sample, everything works fine.

如果我注释 spring.datasource.schema=sample 并取消注释 spring.jpa.properties.hibernate.default_schema=sample,一切正常。

I do not know the differences between those two, anyone can help?

我不知道这两者之间的区别,有人可以帮忙吗?

回答by NA.

spring.datasource.schema is used by Spring boot to load a file with sql into your database. If you use this Postgres will think you want to use the default 'public' schema.

Spring boot 使用 spring.datasource.schema 将带有 sql 的文件加载到数据库中。如果你使用这个 Postgres 会认为你想使用默认的“公共”模式。

spring.jpa.properties.hibernate.default_schema Tells Hibernate which schema in Postgres that you want to use. By setting this per your example, Postgres will use the 'sample' schema.

spring.jpa.properties.hibernate.default_schema 告诉 Hibernate 您要使用 Postgres 中的哪个模式。通过根据您的示例进行设置,Postgres 将使用“示例”模式。