如何在ColdFusion ORM中深层克隆持久实体? [英] How do you deep clone a persistent entity in ColdFusion ORM?
问题描述
我有一个用作模板的持久实体:
I have a persistent entity that I'm using as a template:
Company
Locations
Departments
Employees
换句话说, code>包含许多
位置
,其中包含许多 Departments
,其中包含许多员工
。我有一个公司
设置为模板,应该在创建新公司时复制。但是,此模板在数据库中是持久的。我尝试使用下面的代码深层克隆它:
In other words, a Company
contains many Locations
, which contains many Departments
, which contains many Employees
. I have one Company
set up as a template that should be copied when a new company is created. However, this template is persistent in the database. I tried using the following code to deep clone it:
var template = EntityLoadByPK("Company", 13);
var company = Duplicate(template);
EntitySave(company);
但我收到一个错误说,实体没有附加到会话。所以我试图在保存前分配0到所有的ID:
But I receive an error says that the entity is not attached to the session. So then I tried to assign 0 to all the IDs before saving:
company.setId(0);
for (location in company.getLocations())
{
location.setId(0);
// more nested for loops
}
但是我收到类似的错误。最后,我试图做一个直接的1:1属性的副本:
But I receive a similar error. Finally, I tried to do a direct 1:1 copy of the properties:
var newCompany = EntityNew("Company");
newCompany.setName(company.getName());
newCompany.setCEO(company.getCEO());
// etc...
但是这样会使对象变得越来越麻烦图表。是否有更容易的方法深层克隆一个持久化实体,以便你得到一个全新的瞬态实体,包括其所有的子集合?
But this gets more and more cumbersome the deeper the object graph goes. Is there an easier way of deep cloning a persistent entity so that you get a brand new transient entity, including all of its child collections?
推荐答案
上次我遇到同样的情况,我只是在根CFC中写了一个类似克隆的方法。无法调用 clone
btw,因为它是保留我相信。
last time I encountered the same situation, I just wrote a clone-like method in the root CFC. Cannot call it clone
btw, 'cause it's reserved I believe.
这篇关于如何在ColdFusion ORM中深层克隆持久实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!