Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 619
Chrisma Daniel Avatar asked Jan 29 '16 11:01

Chrisma Daniel


People also ask

Which is better Spring data JPA or Hibernate?

JPA uses EntityManager interface to create/read/delete operation and maintains the persistence context. Hibernate uses Session interface to create/read/delete operation and maintains the persistence context. JPA uses JPQL (Java Persistence Query Language) as Object Oriented Query language for database operations.

What is difference between Spring data JPA and JPA?

Spring data jpa- it is same like jpa means we can describe in below way. Spring Data Jpa is jpa data abstraction access which means it likes a jpa but it add some extra functionality, Without jpa we can not implement the spring data jpa.

How do I decide between JPA and Spring JDBC template?

JDBC is database-dependent, which means that different scripts must be written for different databases. On the other side, JPA is database-agnostic, meaning that the same code can be used in a variety of databases with few (or no) modifications.

What is Spring DataSource schema?

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.


1 Answers

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.

like image 132
NA. Avatar answered Nov 16 '22 23:11

NA.