宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產(chǎn)品動態(tài)
精選推薦

Spring Data JPA自動生成表時列順序混亂的解決辦法

管理 管理 編輯 刪除

最近把Spring Boot的版本升級到了3.3.5,突然發(fā)現(xiàn)一個問題:當(dāng)使用Spring Data JPA自動生成表的時候,所產(chǎn)生的列順序與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;

}























實際自動創(chuàng)建出來的是這樣的:

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

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

#老版本解決方案

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

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

#新版本解決方案

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

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。進(jìn)一步觀察這個新變量的處理過程,可以看到如下邏輯:

c94ce202412231601149378.png

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

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

到這里,在新版本中的這個問題就解決了。如果你也遇到了類似的問題,希望本文對你有所幫助。

請登錄后查看

Lily jar 最后編輯于2024-12-23 16:02:23

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

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

作者 管理員 企業(yè)

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

{{itemf.name}}

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

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

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

相關(guān)推薦

快速安全登錄

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

微信登錄/注冊

切換手機號登錄

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

{{codeText}}
切換微信登錄/注冊
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

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