Chrisma Daniel
Chrisma Daniel

Reputation: 175

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

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

This is my .properties file content:

#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.

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

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

Upvotes: 7

Views: 45683

Answers (1)

NA.
NA.

Reputation: 6579

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.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.

Upvotes: 23

Related Questions