【发布时间】:2017-12-29 17:52:24
【问题描述】:
我的用例如下:我继承了一个使用休眠的项目。就本练习而言,我现在关注的实体不允许修改。练习的目的是用更适合新需求的不相关实现替换旧实体的使用。
目标是能够将功能从旧实体增量移动到新实体。
按原样,遗留实体的使用看起来像
//...
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