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

HHH90000028警告:<hibernate-mappings/>弃用的迁移与配置问题

解决Hibernate HHH90000028警告:<hibernate-mappings>弃用问题

1. 迁移现有配置至orm.xml或mapping.xml

选择目标文件类型

  • orm.xml:遵循JPA标准,兼容性更强,适合需要跨JPA提供商的场景;
  • Hibernate专属mapping.xml:基于Hibernate扩展的XML格式,支持更多Hibernate特有特性,迁移成本相对较低。

迁移至JPA标准orm.xml

  1. 创建orm.xml文件:
    在项目META-INF目录下新建orm.xml,使用JPA标准根元素与命名空间:
    <?xml version="1.0" encoding="UTF-8"?>
    <entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_2.xsd"
                     version="2.2">
        <!-- 迁移后的映射内容 -->
    </entity-mappings>
    
  2. 转换原hbm.xml映射:
    • 原<class>标签替换为<entity>,指定name(实体类全限定名)和table(对应数据库表名);
    • 原<id>标签保留,主键生成策略调整为JPA标准:比如原<generator class="native">改为<generated-value strategy="IDENTITY"/>;
    • 普通属性<property>直接迁移,确保column属性或嵌套<column>标签符合JPA规范;
    • 关联关系(如<many-to-one>、<one-to-many>)对应JPA同名标签,调整fetch、cascade等属性为JPA标准取值(与Hibernate一致的可直接保留)。
  3. 配置Hibernate加载文件:
    在hibernate.cfg.xml中添加映射引用:
    <mapping resource="META-INF/orm.xml"/>
    
    Spring Boot环境可通过spring.jpa.mapping-resources指定文件路径。

迁移至Hibernate专属mapping.xml

  1. 创建Hibernate映射文件:
    新建文件(如hibernate-mapping.xml),使用Hibernate专属命名空间:
    <?xml version="1.0" encoding="UTF-8"?>
    <hibernate-mapping xmlns="http://www.hibernate.org/xsd/orm/hbm"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xsi:schemaLocation="http://www.hibernate.org/xsd/orm/hbm http://www.hibernate.org/xsd/orm/hbm/hbm-mapping-6.0.xsd">
        <!-- 迁移后的映射内容 -->
    </hibernate-mapping>
    
  2. 转换映射内容:
    大部分标签与旧hbm.xml兼容,仅需调整Hibernate 6+弃用的属性:
    • 原<generator class="native">替换为<generated-value strategy="IDENTITY"/>;
    • 移除lazy="true"这类弃用属性,改为fetch="LAZY"。
  3. 加载映射文件:
    在hibernate.cfg.xml中添加:
    <mapping resource="path/to/hibernate-mapping.xml"/>
    

2. 启用hibernate.transform_hbm_xml.enabled配置项

这是临时过渡方案,仅用于快速消除警告,不建议长期依赖,最终仍需完成迁移。

不同环境下的配置方式

  • 原生Hibernate(hibernate.cfg.xml):
    添加配置项:
    <property name="hibernate.transform_hbm_xml.enabled">true</property>
    
  • Spring Boot(application.properties):
    添加配置:
    spring.jpa.properties.hibernate.transform_hbm_xml.enabled=true
    
  • 原生Hibernate代码配置:
    通过Configuration类直接设置:
    Configuration config = new Configuration();
    config.setProperty("hibernate.transform_hbm_xml.enabled", "true");
    config.addResource("resources/hibernate-configs/hibernate-mappings/mappings.hbm.xml");
    SessionFactory sessionFactory = config.buildSessionFactory();
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 20:24:22