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

Chr*_*iel 5 postgresql hibernate spring-data spring-data-jpa spring-boot

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

这是我的.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
Run Code Online (Sandbox Code Playgroud)

我实体类的这一部分:

@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;
    }
Run Code Online (Sandbox Code Playgroud)

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

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

我不知道这两者之间的区别,谁都可以帮忙?

NA.*_*NA. 10

Spring引导使用spring.datasource.schema将带有sql的文件加载到数据库中.如果你使用这个Postgres会认为你想使用默认的'public'模式.

spring.jpa.properties.hibernate.default_schema告诉Hibernate要使用Postgres中的哪个模式.通过根据您的示例设置此项,Postgres将使用"示例"架构.