/ / Spring Boot spring.datasource.schema VS spring.jpa.properties.hibernate.default_schema - hibernate, postgresql, spring-boot, spring-data, spring-data-jpa

Spring Boot spring.datasource.schema VS spring.jpa.properties.hibernate.default_schema - hibernate, postgresql, spring-boot, spring-data, spring-data-jpa

Recentemente, ho iniziato a utilizzare Spring Boot per lo sviluppo di app Web.

Questo è il mio contenuto di file .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

Questa parte della mia classe di entità:

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

Basato su .properties sopra, il problema è che ogni volta che provo a salvare un nuovo SampleInfo usando il repository JPA di Spring Data, ottengo sempre la sequenza di errori "sample_info_seq" non trovata.

Se commento spring.datasource.schema = sample e uncomment spring.jpa.properties.hibernate.default_schema = sample, tutto funziona correttamente.

Non conosco le differenze tra questi due, chiunque può aiutare?

risposte:

5 per risposta № 1

spring.datasource.schema viene utilizzato da Spring Boot per caricare un file con sql nel database. Se usi questo Postgres penseresti di voler usare lo schema "pubblico" predefinito.

spring.jpa.properties.hibernate.default_schema Indica di ibernare quale schema in Postgres si desidera utilizzare. Impostando questo per il tuo esempio, Postgres utilizzerà lo schema "campione".