Hibernate 5 - Auxiliary Database objects not generating in sequence order

I am migrating a project from hibernate 4 to 5.6x. We have user-defined schemas in the hbm mapping files and those auxiliary database schemas should be exported in proper sequence order.

If you see the below one, first it would drop the IND table and next it creates a view with the same name.

<hibernate-mapping package="com.tpk.or">
    <!--  The parent class, whose table will be dropped and replaced by a view. -->
    <class name="INDCount" entity-name="INDCount" table="IND" polymorphism="explicit">
        &orProperties;
        <component name="statsKey" class="com.tpk.or.StatsKey" access="field"
                lazy="false">
            <many-to-one name="dateComp" column="sod"
                class="com.tpk.or.DateComponents"
                not-null="true" foreign-key="ma_sod_fk" lazy="false"/>
            <many-to-one name="timeComp" column="mod"
                class="com.tpk.or.TimeComp"
                not-null="true" foreign-key="ma_tod_fk" lazy="false"/>
            <property name="granularity" type="int" 
                access="com.tpk.or.dao.PropAccessor" not-null="true"/>
        </component>
    </class>

    <database-object>
        <create>DROP TABLE IND</create>
        <drop></drop>
        <dialect-scope name="com.tpk.sqlServerDialect.SQLMappingFix"/>
    </database-object>
    <database-object>
        <create>
        CREATE VIEW IND
        WITH SCHEMABINDING
        AS
        SELECT id, sod, mod, 0 as granularity, 
        COMPONENT_ID, APPLICATION_ID, BRIDGED
        FROM dbo.IND_5MIN
        UNION ALL
        SELECT id, sod, mod, 6, 
        COMPONENT_ID, APPLICATION_ID, BRIDGED
        FROM dbo.IND_30MIN
        </create>
        <drop>DROP VIEW IND</drop>
        <dialect-scope name="com.tpk.sqlServerDialect.SQLMappingFix"/>
    </database-object>
</hibernate-mapping>

In Hib4 Schemaexport class always maintains the order while generating the schemas but in Hib5 auxiliary DB schemas not generating in sequence order.

In our case, create view generate before the drop table schema. (refer below)

CREATE TABLE IND ****
GO
        CREATE VIEW IND
        WITH SCHEMABINDING
        AS
        SELECT id, sod, mod, 0 as granularity, 
        COMPONENT_ID, APPLICATION_ID, BRIDGED
        FROM dbo.IND_5MIN
        UNION ALL
        SELECT id, sod, mod, 6, 
        COMPONENT_ID, APPLICATION_ID, BRIDGED
        FROM dbo.IND_30MIN

GO
DROP TABLE  IND

After debugging the code, I noticed that the auxiliary DB object stores in the hashmap in the Hib5 but in the previous version it stores in ArrayList that’s why the Hib5 auxiliary db object not maintaining the order while exporting.

I don’t see any documentation about this change. Is there any option to maintain the order ?

How to fix/handle this problem ?

AuxiliaryDatabaseObjects

I have a fix for this for Hibernate 6.2, but we won’t backport this. You simply shouldn’t use hbm2ddl for production purposes. Use Liquibase or Flyway to do schema management. hbm2ddl is just a tool to emit a schema that Hibernate expects, but you should always adapt it further to your needs and manage it with one of the schema management tools I mentioned.