You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Spring Boot 3(Hibernate 6)后自定义SchemaFilterProvider失效致表验证报错

问题描述

升级至Spring Boot 3及Hibernate 6后,配置属性hibernate.hbm2ddl.schema_filter_provider无法正常生效。我实现了自定义SchemaFilterProvider以排除my_table的Schema验证,并配置该属性启用,但仍抛出错误:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [my_table]

经调试发现系统始终使用DefaultSchemaFilter.INSTANCE,未加载自定义配置,相关核心代码如下:

验证逻辑代码

protected void validateTables(
        Metadata metadata,
        DatabaseInformation databaseInformation,
        ExecutionOptions options,
        ContributableMatcher contributableInclusionFilter,
        Dialect dialect, Namespace namespace) {

    final NameSpaceTablesInformation tables = databaseInformation.getTablesInformation( namespace );
    for ( Table table : namespace.getTables() ) {
        if ( options.getSchemaFilter().includeTable( table )
                && table.isPhysicalTable()
                && contributableInclusionFilter.matches( table ) ) {
            validateTable(
                    table,
                    tables.getTableInformation( table ),
                    metadata,
                    options,
                    dialect
            );
        }
    }
}

ExecutionOptions构建代码

public static ExecutionOptions buildExecutionOptions(
        final Map<String,Object> configurationValues,
        final ExceptionHandler exceptionHandler) {
    return buildExecutionOptions(
            configurationValues,
            DefaultSchemaFilter.INSTANCE,
            exceptionHandler
    );
}

目前Hibernate存在未解决的相关BUG(HBX-2476),请问如何才能排除my_table的Schema验证?


解决方案

方法1:精准关闭单个实体的验证

如果my_table对应有实体类,直接在实体上添加Hibernate专属注解,跳过该实体的Schema验证:

@Entity
@Table(name = "my_table")
@org.hibernate.annotations.Schema(validationMode = SchemaValidationMode.NONE)
public class MyEntity {
    // 实体字段及方法
}

这种方式只针对目标表生效,不会影响其他表的验证逻辑,是最推荐的临时方案。

方法2:通过编程方式注入自定义SchemaFilter

绕过失效的hibernate.hbm2ddl.schema_filter_provider配置,直接通过Spring配置注入自定义SchemaFilter:

  1. 实现自定义SchemaFilter:
public class CustomSchemaFilter implements SchemaFilter {
    @Override
    public boolean includeTable(Table table) {
        // 排除my_table的验证
        return !"my_table".equals(table.getName());
    }

    @Override
    public boolean includeSequence(Sequence sequence) {
        return true;
    }

    @Override
    public boolean includeCatalog(Name catalog) {
        return true;
    }

    @Override
    public boolean includeSchema(Name schema) {
        return true;
    }
}
  1. 配置HibernatePropertiesCustomizer注入该过滤器:
@Configuration
public class HibernateCustomConfig {
    @Bean
    public HibernatePropertiesCustomizer hibernatePropertiesCustomizer() {
        return properties -> properties.put("hibernate.schema_filter", new CustomSchemaFilter());
    }
}

注意:此方式依赖Hibernate内部配置属性,后续版本可能变动,需关注HBX-2476的修复进度。

方法3:利用schema分组过滤

给目标表指定一个专属schema,然后通过全局配置跳过该schema的验证:

  1. 修改实体类的@Table注解:
@Entity
@Table(name = "my_table", schema = "ignored_schema")
public class MyEntity {
    // 实体字段及方法
}
  1. 在application.properties/application.yml中添加配置:
hibernate.schema.validation.group.default=!ignored_schema

这种方式适合需要批量排除某个schema下所有表的场景。


内容的提问来源于stack exchange,提问作者Ayelet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 07:52:16