<noframes id="bhrfl"><address id="bhrfl"></address>

    <address id="bhrfl"></address>

    <noframes id="bhrfl"><address id="bhrfl"><th id="bhrfl"></th></address>

    <form id="bhrfl"><th id="bhrfl"><progress id="bhrfl"></progress></th></form>

    <em id="bhrfl"><span id="bhrfl"></span></em>

    全部
    常見問題
    產品動態
    精選推薦

    Spring Data JPA自動生成表時列順序混亂的解決辦法(最新版)

    管理 管理 編輯 刪除

    最近把Spring Boot的版本升級到了3.3.5,突然發現一個問題:當使用Spring Data JPA自動生成表的時候,所產生的列順序與Entity類中的變量順序不一致了。比如,有一個下面這樣的Entity:


    @Data
    @Entity(name = "t_config")
    @EntityListeners(AuditingEntityListener.class)
    public class Config {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @Column(length = 20)
        private String itemKey;
        @Column(length = 200)
        private String itemValue;
        @Column(length = 200)
        private String itemDesc;
    
        @CreatedDate
        private Date createTime;
        @LastModifiedDate
        private Date modifyTime;
    
    }
    
    

    實際自動創建出來的是這樣的:

    自動創建的表結構中各個列與Entity類中的變量順序不一致。其實該問題是一個老生常談的問題了,在這次升級的工程里是有做過解決方案的。只是升級了Spring Boot版本之后,之前的解決方案失效了。

    搜索了一番,同時還問了一下AI,發現給出的方案還都是老的解決方案,所以今天特別寫一篇來記錄下新版本之下,要如何解決這個問題。如果您剛好遇到類似的問題,可以參考本文來解決。

    #老版本解決方案

    新老版本的解決思路是類似的,都是替換Hibernate的實現,下面是老版本的解決步驟:

    1. 在工程中新建org.hibernate.cfg
    2. 找到hibernate-core包下的org.hibernate.cfg下的PropertyContainer類,復制到本工程的org.hibernate.cfg包下
    3. PropertyContainer類中定義的persistentAttributeMap類型從TreeMap修改為LinkedHashMap

    #新版本解決方案

    雖然之前的方案失效了,但思路應該還是對的,所以第一反應是看看當前版本下的PropertyContainer類,具體如下(省略了一些不重要的內容):


    package org.hibernate.boot.model.internal;
    
    //省略...
    
    public class PropertyContainer {
    
        private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, PropertyContainer.class.getName());
    
        /**
         * The class for which this container is created.
         */
        private final XClass xClass;
        private final XClass entityAtStake;
    
        /**
         * Holds the AccessType indicated for use at the class/container-level for cases where persistent attribute
         * did not specify.
         */
        private final AccessType classLevelAccessType;
    
        private final List<XProperty> persistentAttributes;
    
    	//省略...
    
    }
    
    
    

    可以看到有兩個重要變化部分:

    1. PropertyContainer類的包名從org.hibernate.cfg改到了org.hibernate.boot.model.internal
    2. 之前的TreeMap<String XProperty> persistentAttributeMap變量沒有了,但多了一個List<XProperty> persistentAttributes。進一步觀察這個新變量的處理過程,可以看到如下邏輯:

    所以,新版的方案就以下兩個步驟:

    1. 在工程中新建org.hibernate.boot.model.internal
    2. 找到hibernate-core包下的org.hibernate.boot.model.internal下的PropertyContainer類,復制到本工程的org.hibernate.boot.model.internal包下
    3. PropertyContainer類中,上面圖中紅色圈出部門定義的localAttributeMap = new TreeMap<>();修改為localAttributeMap = new LinkedHashMap<>();


    轉載: https://spring.didispace.com/spring-boot-2/4-11-jpa-column-order.html#%E6%96%B0%E7%89%88%E6%9C%AC%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88

    請登錄后查看

    CRMEB 最后編輯于2025-02-24 15:31:29

    快捷回復
    回復
    回復
    回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
    排序 默認正序 回復倒序 點贊倒序

    {{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

    作者 管理員 企業

    {{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
    {{item.is_suggest == 1? '取消推薦': '推薦'}}
    沙發 板凳 地板 {{item.floor}}#
    {{item.user_info.title || '暫無簡介'}}
    附件

    {{itemf.name}}

    {{item.created_at}}  {{item.ip_address}}
    打賞
    已打賞¥{{item.reward_price}}
    {{item.like_count}}
    {{item.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復

    {{itemc.user_info.nickname}}

    {{itemc.user_name}}

    回復 {{itemc.comment_user_info.nickname}}

    附件

    {{itemf.name}}

    {{itemc.created_at}}
    打賞
    已打賞¥{{itemc.reward_price}}
    {{itemc.like_count}}
    {{itemc.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復
    查看更多
    打賞
    已打賞¥{{reward_price}}
    449
    {{like_count}}
    {{collect_count}}
    添加回復 ({{post_count}})

    相關推薦

    快速安全登錄

    使用微信掃碼登錄
    {{item.label}} 加精
    {{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態 精選推薦 首頁頭條 首頁動態 首頁推薦
    取 消 確 定
    回復
    回復
    問題:
    問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
    答案:
    提交
    bug 需求 取 消 確 定
    打賞金額
    當前余額:¥{{rewardUserInfo.reward_price}}
    {{item.price}}元
    請輸入 0.1-{{reward_max_price}} 范圍內的數值
    打賞成功
    ¥{{price}}
    完成 確認打賞

    微信登錄/注冊

    切換手機號登錄

    {{ bind_phone ? '綁定手機' : '手機登錄'}}

    {{codeText}}
    切換微信登錄/注冊
    暫不綁定
    亚洲欧美字幕
    CRMEB客服

    CRMEB咨詢熱線 咨詢熱線

    400-8888-794

    微信掃碼咨詢

    CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
    返回頂部 返回頂部
    CRMEB客服