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

标签 hibernate postgresql spring-boot spring-data spring-data-jpa

最近,我开始使用 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

我的实体类的这一部分:

@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,问题是每次我尝试使用 Spring Data JPA 存储库保存新的 SampleInfo 时,我总是会收到错误序列“sample_info_seq”未找到。

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

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

最佳答案

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

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

关于hibernate - Spring Boot spring.datasource.schema VS spring.jpa.properties.hibernate.default_schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35082968/

相关文章:

java - 警告[CommonsLogger.java :60] ognl. MethodFailedException : Method failed for object [java. lang.NoSuchMethodException :([Ljava. lang.String;)]

java - json转换期间出现stackoverflow错误(hibernate双向映射)

sql - 使用一对一的键快速合并表

memory-management - Spring Boot 默认内存设置是什么?

java - Spring - 将任务安排在某个时间段

java - 如果 RequestBody 中存在任何未知字段,则需要拒绝 POST/PUT 请求

java - Hibernate 逆向工程使用 CustomReverseEngineeringStrategy 类删除目录名称

java - Hibernate 查询未映射的连接表

postgresql - 生成重复的唯一约束名称

postgresql - Postgres 9.6 : Parallel query does not take max_parallel_workers_per_gather setting