gpt4 book ai didi

java - 如何重构 hibernate 实体?

转载 作者:太空宇宙 更新时间:2023-11-04 10:49:13 25 4
gpt4 key购买 nike

我的用例如下:我继承了一个使用 hibernate 的项目。出于本练习的目的,我现在关注的实体禁止修改。练习的目的是用更适合新需求的不相关实现替换旧实体的使用。

目标是能够增量地将功能从旧实体转移到新实体。

按原样,遗留实体的使用看起来像这样

    //...

final Session currentSession = sessionFactory().getCurrentSession();
{
LegacyEntity oldAndBusted = get(currentSession, "12345");

oldAndBusted.change(...);

put(oldAndBusted);
}
}

LegacyEntity get(final Session currentSession, String businessId) {
return (LegacyEntity) currentSession
.createQuery("from PurpleMonkeyDishwasher where businessId = ?")
.setParameter(0, "12345")
.uniqueResult();
}

void put(final Session currentSession, LegacyEntity changed) {
currentSession.saveOrUpdate(changed);
}

配置魔法隐藏在某些 hbm.xml 文件中

<class name="LegacyEntity" table="PurpleMonkeyDiswasher">
<!-- stuff -->
</class>

如何为映射到同一个表的新实体安排类似的代码

BuzzwordCompliantEntity get(final Session currentSession, String businessId);
void put(BuzzwordCompliantEntity changed);

不破坏同一进程中仍在使用 LegacyEntity 的代码路径?

最佳答案

“出于本次练习的目的,我现在关注的实体是关闭修改的......目标是能够逐步将功能从旧实体转移到新实体。”我觉得这很矛盾。当用另一个类替换一个类时,我总是通过以下方式取得成功:1) 逐步将旧类 API 更改为新类 API 的子集,2) 重命名旧类型,使其与新类具有相同的名称和包,3) 删除旧类(我们在步骤 2 中重命名)。在完成所有这些工作时,我尽可能依赖 IDE 的重构功能。

关于java - 如何重构 hibernate 实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48026454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com