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?
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.
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.
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.
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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With