1

最近,我开始使用Spring Boot进行Web应用程序开发。Spring Boot spring.datasource.schema VS spring.jpa.properties.hibernate.default_schema

这是我的属性文件内容:

#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 

我的实体类的这一部分:

@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; 
    } 

基于以上的.properties,这个问题是每一次我尝试保存新SampleInfo时间使用Spring Data JPA存储库,我总是得到错误序列“sample_info_seq”not found。

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

我不知道这两者之间的差异,任何人都可以提供帮助吗?

回答

4

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

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