Spring Boot JPA Hibernate将分区表Map到实体

6yt4nkrj  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(142)

我有一个Sping Boot 应用程序,我在其中连接到PostgreSQL数据库。(idactive_flag),并且表在其中一个上分区。当我将表Map到Java实体时,我总是在主表上得到一个错误,它丢失了,但是分区表我可以正常Map它们而没有任何错误。这是正常的行为吗?我们可以不Map主分区表吗?
下面是我的表和类Map

create table test_kc (
    test_id integer not null,
    name text not null,
    active_flag boolean not null default true,
    created_at timestamp not null default now(),

    constraint test_kc_pk primary key (test_id, active_flag)
) partition by list (active_flag);

create table test_kc_active partition of test_kc for values in (true);
create table test_kc_inactive partition of test_kc for values in (false);

字符串
EntityId.java

@Data
public class EntityId implements Serializable {
    private Integer id;
    private Boolean activeFlag;
}


TestKcEntity.java

@Entity
@Table(name = "test_kc")
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@IdClass(EntityId.class)
public final class TestKcEntity {
    @Id
    @Column(name = "test_id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = false)
    private String name;

    @Id
    @Column(name = "active_flag", nullable = false)
    private Boolean activeFlag;

    @Column(name = "created_at", nullable = false)
    private OffsetDateTime createdAt;
}


从上面的代码中可以看出,当表名为test_kc时,我在运行应用程序时得到以下错误:Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [test_kc]
当我将表名替换为test_kc_active时,它工作正常,应用程序运行正常。

rsaldnfx

rsaldnfx1#

实际的解决方案是将休眠参数hibernate.hbm2ddl.extra_physical_table_types设置为PARTITIONED TABLE。这样一来,Hibernate将不仅接受表和视图作为实体@Table的值。
另见https://docs.jboss.org/hibernate/orm/6.3/userguide/html_single/Hibernate_User_Guide.html#settings-schema

相关问题