我如何重构hibernate实体?

问题描述 投票:1回答:1

我的用例如下:我继承了一个使用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的代码路径?

java hibernate refactoring
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.