I created a table with the following code:
CREATE TABLE my_table(
id BIGSERIAL,
PRIMARY KEY(id)
) PARTITION BY RANGE (id);
CREATE TABLE my_table_2023_10 PARTITION OF my_table FOR VALUES FROM (1) to (100000);
application.yml:
server:
port: ${SERVER_PORT:12332}
spring:
datasource:
url: jdbc:postgresql://localhost:5432/pg_test
username: postgres
password: 12345
driverClassName: org.postgresql.Driver
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate.ddl-auto: validate
Entity
@Entity(name = "my_table")
@Data
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.12'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.spring.playground'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.11'
implementation 'org.postgresql:postgresql:42.6.0'
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
}
When I start my app, I get the following exception: Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [my_table]
It works fine if I replace hibernate.ddl-auto: validate by hibernate.ddl-auto: none
With ddl-auto: none hibernate inserts and updates work fine.
I've found a similar question : JPA Hibernate Mapping Partitioned Table to an Entity
But it does not have an answer.
To solve this problem you have to change a little bit Entity Class
:
@Entity
@Table(name = "my_table")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database. The @Table annotation provides four attributes, allowing you to override the name of the table, its catalog, and its schema, and enforce unique constraints on columns in the table.
In your case it is the reason, because MyEntity
is not table name in database.
Update
As a fix
You have to add next one Param in config file
hibernate.hbm2ddl.extra_physical_table_types=PARTITIONED TABLE