Hibernate 6 调整字段生成顺序(按类属性顺序)

发布时间 2023-10-26 01:46:06作者: 一年`

升级Hibernate到版本6之后,原本修改字段生成顺序的方法失效了,需要更改另一个类。

Hibernate 5

修改org.hibernate.cfg下的PropertyContainer类,将其中的TreeMap改为linkedHashMap

Hibernate 6

此处使用的使6.2.9
修改org.hibernate.boot.model.internal下的PropertyContainer类,去掉其中的判断,直接赋值为LinkedHashMap类型。

        final Map<String, XProperty> localAttributeMap;
        // If the record class has only record components which match up with fields and no additional getters,
        // we can retain the property order, to match up with the record component order
        if ( !recordComponents.isEmpty() && recordComponents.size() == fields.size() && getters.isEmpty() ) {
            localAttributeMap = new LinkedHashMap<>();
        }
        //otherwise we sort them in alphabetical order, since this is at least deterministic
        else {
            localAttributeMap = new TreeMap<>();
        }

替换为

        final Map<String, XProperty> localAttributeMap = new LinkedHashMap<>();

其他

无需变更源码,直接在项目下新建软件包org.hibernate.boot.model.internal,把对应源码的类复制过去,修改即可。